Android图片旋转

Android中,我们可以使用矩阵实现图像旋转

首先,创建一个布局xml文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?><br />  
  2. <LinearLayout android:id="@+id/LinearLayout01"<br />  
  3. android:layout_width="fill_parent"<br />  
  4. android:layout_height="fill_parent"<br />  
  5. xmlns:android="http://schemas.android.com/apk/res/android"<br />  
  6. android:background="#ffffff"<br />  
  7. android:gravity="center"><br />  
  8. <ImageView android:id="@+id/ImageView01"<br />  
  9. android:layout_width="wrap_content"<br />  
  10. android:layout_height="wrap_content"<br />  
  11. android:src="@drawable/refresh" /><br />  
  12. </LinearLayout><br />  



创建主Activity类文件:

[java]  view plain copy
  1. public class ExampleApp extends Activity  
  2. {  
  3. private ImageView img;  
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {  
  6. super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.main);  
  8. img=(ImageView)findViewById(R.id.ImageView01);  
  9. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);  
  10. // Getting width & height of the given image.  
  11. int w = bmp.getWidth();  
  12. int h = bmp.getHeight();  
  13. // Setting post rotate to 90  
  14. Matrix mtx = new Matrix();  
  15. mtx.postRotate(90);  
  16. // Rotating Bitmap  
  17. Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 00, w, h, mtx, true);  
  18. BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);  
  19. img.setImageDrawable(bmd);  
  20. }  
  21. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Android中的属性动画(Property Animation)来实现图片旋转动画。具体实现步骤如下: 1.在res/anim文件夹下创建rotate.xml文件,内容如下: ```xml <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:repeatCount="0" android:duration="1000" /> ``` 其中,fromDegrees和toDegrees分别表示旋转的起点角度和终点角度;pivotX和pivotY表示旋转中心点的位置;repeatCount表示动画重复次数,这里设置为0表示不重复;duration表示动画的持续时间,这里设置为1000毫秒。 2.在代码中找到要进行旋转动画的ImageView控件,使用如下代码进行动画设置: ```java ImageView imageView = findViewById(R.id.image_view); ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360); rotationAnimator.setDuration(1000); rotationAnimator.setRepeatCount(ValueAnimator.INFINITE); rotationAnimator.setInterpolator(new LinearInterpolator()); rotationAnimator.start(); ``` 其中,R.id.image_view表示ImageView控件的ID;ObjectAnimator是属性动画中的旋转动画,ofFloat方法中的第一个参数表示要进行动画的控件,第二个参数表示动画类型(这里是旋转),第三个参数表示旋转起始角度,第四个参数表示旋转终止角度;setDuration方法表示动画持续时间;setRepeatCount方法设置动画重复次数,这里设置为无限循环;setInterpolator方法设置动画插值器,这里设置为线性插值器;start方法开始动画。 这样就可以实现图片旋转动画了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值