how to rotate an image back and forth in android without using animation
i want to create a car dashboard on android.Actually i am created already this application.But that is based on animation and it is working nicely.Now here i want to add two button(up and down).when i am clicking up the hand have to move 10 degree up.Again click up button means the hand have to move 10 degree up from the current position, and when i am clicking down means it have to come down by 10 degree. i cant do this by a single button in animation.in animation i want to create more b开发者_JAVA百科utton and each button i want to create different animation. This is very complex. please somebody help me to do this.Here i am adding my code
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class dash1 extends Activity {
ImageView img,img1;
Animation an,an1,an2,an3;Button bt,bt2,bt3,bt4;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.ImageView01);
final Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.minute);
final int w= bmp.getWidth();
final int h= bmp.getHeight();
final Matrix mat = new Matrix();
mat.postRotate(180);
Bitmap rtd = Bitmap.createBitmap(bmp, 0, 0, w, h, mat, true);
BitmapDrawable bmd = new BitmapDrawable(rtd);
img.setImageDrawable(bmd);
an = AnimationUtils.loadAnimation(this, R.anim.anim);
an1 = AnimationUtils.loadAnimation(this, R.anim.anim1);
an2 = AnimationUtils.loadAnimation(this, R.anim.anim2);
an3 = AnimationUtils.loadAnimation(this, R.anim.anim3);
bt = (Button)findViewById(R.id.Button01);
bt2 = (Button)findViewById(R.id.Button02);
bt3 = (Button)findViewById(R.id.Button03);
bt4 = (Button)findViewById(R.id.Button04);
bt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
an.setFillAfter(true);
img.startAnimation(an);
return false;
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.startAnimation(an1);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
an2.setFillAfter(true);
img.startAnimation(an2);
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
an3.setFillAfter(true);
img.startAnimation(an3);
}
});
}
}
You basically have everything you need. You just need to change the rotation on the matrix by 10 degrees and apply that to the image you use for your ImageView. As you already rotate the image with 180° you should be familiar with what you need to rotate again 10 degrees...
Sample code:
public boolean onTouch(View v, MotionEvent event) {
mat.postRotate(10); // or -10 degree
Bitmap rtd = Bitmap.createBitmap(bmp, 0, 0, w, h, mat, true);
BitmapDrawable bmd = new BitmapDrawable(rtd);
img.setImageDrawable(bmd);
}
精彩评论