如何在Android中不使用动画来回旋转图像

问题描述:

我想在android上创建一个汽车仪表板。其实我已经创建了这个应用程序。但是这是基于动画的,它工作的很好。现在,我想要添加两个按钮(向上和向下)。当我点击时,手必须向上移动10度。再次点击按钮意味着手必须从当前位置向上移动10度,并且当我点击时意味着它必须下降10度。我不能做这个动画中的一个按钮。我想要创建更多的按钮和每个按钮,我想创建不同的动画。这非常复杂。请有人帮助我做到这一点。在这里我添加我的代码如何在Android中不使用动画来回旋转图像

 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); 
       } 
      }); 
} 


} 
+1

请再具体些。你想看一个动画,但你想不使用动画api完成,或者你只是想显示一个图像旋转一次x度? – WarrenFaith 2011-02-15 11:16:17

你基本上有你需要的一切。您只需将矩阵上的旋转角度改变10度,然后将其应用于用于ImageView的图像。正如你已经用旋转180度的图像,你应该熟悉你需要再次旋转10度是什么?

示例代码:

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); 
} 
+0

当我使用我们不能看到运动。然后,为什么我在这里使用此代码意味着原始手形图显示方向up.This图像我放在另一个仪表板量规图像。在那里阅读从下往上。这就是为什么我使用该代码保持手图像的起始位置... – khan 2011-02-15 12:41:27