How to resize image by using seekbar? - android

I want to resize image by using seekbar. Seekbar is working but its not giving any good results. With the following code, the image is resized but just when you change the progress of seekbar for the first time. Please help
Seekbar listener
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
// TODO Auto-generated method stub
progresValue = progress;
resizeImage(image, progresValue);
}
});
Function to resize image
public void resizeImage(Bitmap bitmap, int progress) {
Bitmap bitmapSource = bitmap;
float bHeight = bitmapSource.getHeight();
float bWidth = bitmapSource.getWidth();
float factorH = progress / (float) bHeight;
float factorW = progress / (float) bWidth;
float factorToUse = (factorH > factorW) ? factorW : factorH;
Bitmap bm = Bitmap.createScaledBitmap(bitmapSource,
(int)(bWidth * factorToUse), (int)(bHeight * factorToUse),
false);
qImage.setImageBitmap(bm);
}

final SeekBar sk1 = (SeekBar) findViewById(R.id.seekBar1);
sk1.setMax(500);
sk1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(progress,progress);
img.setLayoutParams(params);
img.setMinimumWidth(progress);
img.setMinimumHeight(progress);
}
});

Related

Controlling the frame animation speed with seek bar in android

I have the following code that changes the opacity of the frame animation with the help of a seekbar, now i'm trying to control the duration of frame animation with the help of this seek bar, can anybody help me in telling that if it is possible?
The code:
ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
//Create a new AnimationDrawable
final AnimationDrawable myAnimationDrawable
= createAnimationDrawable();
//apply the new AnimationDrawable
myAnimation.setImageDrawable(myAnimationDrawable);
SeekBar setAnimationAlpha = (SeekBar)findViewById(R.id.setalpha);
Button startAnimation = (Button)findViewById(R.id.startanimation);
Button stopAnimation = (Button)findViewById(R.id.stopanimation);
startAnimation.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!myAnimationDrawable.isRunning()){
myAnimationDrawable.start();
}
}});
stopAnimation.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(myAnimationDrawable.isRunning()){
myAnimationDrawable.stop();
}
}});
setAnimationAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myAnimationDrawable.setAlpha(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}});
}
private AnimationDrawable createAnimationDrawable(){
AnimationDrawable newAnim = new AnimationDrawable();
newAnim.addFrame(getResources().getDrawable(R.drawable.android_1), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_2), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_3), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_4), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_5), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_6), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_7), 90);
newAnim.setOneShot(false);
return newAnim;
}
}

screen brightness seekbar not working

In my app I added a SeekBar which controls the screen brightness. It works some what fine .
I get the device's screen brightness in Oncreate() and set the progress of the SeekBar.
But when I change the seekbar progress , when I set it to the minimum(ie. to the zero position) the device gets stuck and I had to restart my device to get it working again.
What is wring with my code. Please help me
in oncreate() I have the following code,
try {
BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);
brightnessscrollbar.setProgress(BackLightValue);
System.out.println("Baclightvalue is "+BackLightValue);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekbarclicklistener
brightnessscrollbar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
});
I came to know that Device gets stuck at the value zero
So I gave a condition on onProgresschanged as folows
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
if(arg1>5){
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
}
And it works now, like a charm.
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
progress = progressValue;
if(progress <1 ) {
progress = 1;
}
android.provider.Settings.System.putInt(getActivity().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getActivity().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = progress / (float)255;
window.setAttributes(layoutParams);
}

Killing a Sprite in AndEngine

Hi im having a bit of problems getting my sprite to fade back in. Im using paralell entity modifiers, scaling and fading after which the sprite gets new X and Y cordinates and fades in and scales back up. It doesnt work and i think it might have something to do with the onUpdate method.
XOsprite = new Sprite(x, y, XO, engine.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent te, final float xVal,
final float yVal) {
XOsprite.registerEntityModifier(downOut); //kill sprite
isKilled = true;
XOsprite.registerUpdateHandler(new IUpdateHandler() {
#Override
public void reset() {
// TODO Auto-generated method stub
}
#Override
public void onUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stub
totalElapsedTime += pSecondsElapsed;
if(totalElapsedTime >= 3.0f){
BaseGameActivity gameActivity = (BaseGameActivity) activity;
gameActivity.runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(isKilled){
reviveSprite();
isKilled = false;
}
}
});
}
//reset();
}
});
return true;
}
};
XOsprite.registerEntityModifier(inUp);
gameScene.attachChild(XOsprite);
gameScene.registerTouchArea(XOsprite);
return gameScene;
}
-edit- more code
public void reviveSprite(){
setSprites();
XOsprite.unregisterEntityModifier(downOut);
XOsprite.registerEntityModifier(inUp);
}
public void setSprites(){
if (rand.nextInt(2) == 0) {
XO = X;
} else {
XO = O;
}
x = rand.nextInt(MainActivity.CAM_WIDTH);
y = rand.nextInt(MainActivity.CAM_HEIGHT);
}
you are already on the UpdateThread, so no need to do this
gameActivity.runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(isKilled){
reviveSprite();
isKilled = false;
}
}
});
just do this
if(isKilled){
reviveSprite();
isKilled = false;
}

Seek Bar For Zooming In-Out for image

I want to implement a zoom in and zoom out an image using seek bar. If I go to right in Seek Bar image should be zoom in and vice versa.
help me.
you can take look here. http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847
EDITED:
checkout this how-can-i-get-zoom-functionality-for-images.
Here pos is the selected image since i have more than one image to zoom in and zoom out.
public void zoomImage(int pos)
{
if(pos==1) {
seekbar.setMax((int) 60.0f);
seekbar.setProgress(seekvalue4 * 10);
System.out.println("--------seekvalue is :" + seekvalue4 * 10);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float scale = (progress / 10.0f);
//
if (scale <= 0.1f) {
return;
}
iv1.setZoom(scale);
seekvalue4 = (int) scale;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
});
}

thread is throwing error (frame animation)

I am working on frame animation in which i am using thread but thread is throwing me error .
my code is as follow ....
public class Newton_LawsActivity extends Activity implements OnTouchListener, OnGestureListener,OnDoubleTapListener {
/** Called when the activity is first created. */
OnTouchListener l;
ImageView animation;
ImageView hideimage;
TextView t;
Thread timer;
int Rid;
double pixel;
String law="null";
private GestureDetector gestureDetector;
float x,y;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this);
// the frame-by-frame animation defined as a xml file within the drawable folder
animation = (ImageView)findViewById(R.id.image);
//animation.setBackgroundResource(R.drawable.anatomy_5);
hideimage=(ImageView)findViewById(R.id.imagein);
t=(TextView) findViewById(R.id.t);
animation.setBackgroundResource(R.anim.startanimation);
final AnimationDrawable newtonAnimation = (AnimationDrawable) animation.getBackground();
AnimationStart(newtonAnimation);
animation.setOnTouchListener(this);
}
public void AnimationStart(final AnimationDrawable newanimation){
timer=new Thread(){
#Override
public void run(){
try{
}catch(Exception e){}
finally{
Newton_LawsActivity.this.runOnUiThread(new Runnable() {
public void run(){
newanimation.start();
}});
}
}
};
timer.start();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(this, "animation", Toast.LENGTH_LONG).show();
return gestureDetector.onTouchEvent(event);
}
private void animationswitch(float x, float y) {
// TODO Auto-generated method stub
pixel = getmaskpixel(x,y,animation,hideimage);
Log.w("DEBUG","which is null:pixel" + pixel );
if (pixel==-583672){
animation.setBackgroundResource(R.anim.firstlaw_01);
final AnimationDrawable firstlaw_01 = (AnimationDrawable) animation.getBackground();
law="firstlaw_02";
firstlaw_01.start();
animationstop_01();
}
t.setText(Double.toString(pixel));
}
private double getmaskpixel(float x, float y, ImageView view,ImageView hideimage) {
// TODO Auto-generated method stub
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.touchnewtonfinal415);
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) view.getWidth()) / width;
float scaleHeight = ((float) view.getHeight()) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(90);
// recreate the new Bitmap
Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height,matrix, false);
Log.w("DEBUG","which is null:image " + hideimage.getWidth() + " OR " +hideimage.getHeight() );
double bmWidth = view.getWidth();
double bmHeight = view.getHeight();
if ( x < 0 || y < 0 || x > hideimage.getWidth() || y >hideimage.getHeight()){
return 0; //Invalid, return 0
}else{
//Convert touched x, y on View to on Bitmap
int xBm = (int)(x * (bmWidth / hideimage.getWidth()));
int yBm = (int)(y * (bmHeight / hideimage.getHeight()));
return bitmap.getPixel((int)xBm,(int) yBm);
}
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
// TODO Auto-generated method stub
}
#Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y= e.getY();
Log.w("DEBUG","which is null:image " + x + " OR " +y);
animationswitch(x, y);
return true;
}
void animationstop_01(){
if(law=="firstlaw_02"){
timer=new Thread(){
#Override
public void run(){
try{
Thread.sleep(8750);
}catch(Exception e){}
finally{
Log.w("debug","it is firstlaw_02");
animation.setBackgroundResource(R.anim.firstlaw_02);
final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
law="firstlaw_03";
AnimationStart(firstlaw_02);
}};
};
timer.start();
}
}
}
it is throwing me following error..
06-06 14:15:00.668: E/AndroidRuntime(966): FATAL EXCEPTION: Thread-10
06-06 14:15:00.668: E/AndroidRuntime(966): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.invalidate(View.java:5139)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundDrawable(View.java:7486)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundResource(View.java:7395)
06-06 14:15:00.668: E/AndroidRuntime(966): at Newtons.law.Newton_LawsActivity$2.run(Newton_LawsActivity.java:183)
Change your animationstop_01 method to:
void animationstop_01(){
if(law=="firstlaw_02"){
timer=new Thread(){
#Override
public void run(){
try{
Thread.sleep(8750);
}catch(Exception e){
}finally{
Log.w("debug","it is firstlaw_02");
Newton_LawsActivity.this.runOnUiThread(new Runnable() {
public void run(){
animation.setBackgroundResource(R.anim.firstlaw_02);
final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
law="firstlaw_03";
AnimationStart(firstlaw_02);
}
});
}
};
};
timer.start();
}
}
Error is inside the mehod name animationstop_01() where you are trying,
animation.setBackgroundResource(R.anim.firstlaw_02);
put that inside runOnUiThread()
try this:
animation.post(new Runnable() {
public void run() {
animation.setBackgroundResource(R.anim.firstlaw_02);
}
});
In that way you are posting it to the UI thread. View can only be changed from the original thread that created the View.

Categories

Resources