您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关怎么在Android利用Matrix对图片进行旋转,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
自定义一个View,用来控制这两个图片的旋转。com.oyp.loadingdisk.LoadingDiscView.java
package com.oyp.loadingdisk; import java.io.InputStream; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.view.View; /** * 自定义的View,用来显示加载的图片 * @author ouyangpeng * @link http://blog.csdn.net/ouyang_peng * * <p>在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿。<br> * 方法一:给Paint加上抗锯齿标志。然后将Paint对象作为参数传给canvas的绘制方法。<br> * 如:mypaint.setAntiAlias(true);<p> * 方法二:给Canvas加上抗锯齿标志。有些地方不能用paint的,就直接给canvas加抗锯齿,更方便。<br> * 如: * mSetfil = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);<br> * canvas.setDrawFilter(mSetfil); */ public class LoadingDiscView extends View { private RefreshHandle refreshHandle; private Context context; /** 用于旋转的bitmap*/ private Bitmap m_bmp_disc = null; private Matrix m_matrix_disc = new Matrix(); /** 用于展现高亮背景的bitmap*/ private Bitmap m_bmp_light = null; private Matrix m_matrix_light = new Matrix(); /**Paint滤波器*/ private PaintFlagsDrawFilter mSetfil = null; /**声明一个画笔*/ private Paint mypaint = null; /**图像缩放比例*/ private float m_scale =1.0f; /**图像旋转的速度*/ private float m_disc_rot_speed = 0; /**图像旋转的状态*/ private int m_state_play = 1; /**图像旋转的最大速度*/ private float m_disc_max = 20f; public void setRefreshHandle(RefreshHandle refreshHandle) { this.refreshHandle = refreshHandle; } public LoadingDiscView(Context context) { super(context); this.context = context; mSetfil = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);//设置画布绘图无锯齿 initBitmap(); } public boolean initBitmap() { mypaint = new Paint(); //给Paint加上抗锯齿标志 mypaint.setAntiAlias(true);//画笔的抗锯齿(用于线条等) Resources res = context.getResources(); InputStream is = res.openRawResource(R.drawable.loading_disc); m_bmp_disc = BitmapFactory.decodeStream(is); matrixPostTranslate(m_matrix_disc,m_bmp_disc); is = res.openRawResource(R.drawable.loading_light); m_bmp_light = BitmapFactory.decodeStream(is); matrixPostTranslate(m_matrix_light,m_bmp_light); return true; } /** * 旋转图像 * @param matrix 控制旋转的矩阵 * @param bitmap 要旋转的图像 */ private void matrixPostTranslate(Matrix matrix,Bitmap bitmap) { int tmp_width = bitmap.getWidth(); int tmp_height = bitmap.getHeight(); matrix.postTranslate(-tmp_width / 2, -tmp_height / 2); //设置平移位置 matrix.postScale(m_scale, m_scale); //设置缩放比例 matrix.postTranslate(123 * m_scale, 146 * m_scale); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); //给Canvas加上抗锯齿标志 canvas.setDrawFilter(mSetfil);//图片线条(通用)的抗锯齿 canvas.drawBitmap(m_bmp_disc, m_matrix_disc, mypaint); canvas.drawBitmap(m_bmp_light, m_matrix_light, mypaint); } public void update() { if (m_disc_rot_speed > 0.01 || m_state_play == 1){ if (m_state_play == 1 && m_disc_rot_speed<m_disc_max){ m_disc_rot_speed += (m_disc_max+0.5f-m_disc_rot_speed)/30; } else if (m_disc_rot_speed>0.1){ m_disc_rot_speed -= (m_disc_rot_speed)/40; } m_matrix_disc .postRotate(m_disc_rot_speed, 123*m_scale, 146*m_scale); invalidate(); } } public void onPause(){ refreshHandle.stop(); } public void onResume(){ refreshHandle.run(); } }
step3、写一个Handler用来控制图片的旋转 com.oyp.loadingdisk.RefreshHandle.java
package com.oyp.loadingdisk; import android.os.Handler; import android.os.Message; /** * 用来发送消息和处理消息的 * @author ouyangpeng * @link http://blog.csdn.net/ouyang_peng */ public class RefreshHandle extends Handler { LoadingDiscView loadingDiscView; public RefreshHandle(LoadingDiscView loadingDiscView) { this.loadingDiscView = loadingDiscView; loadingDiscView.setRefreshHandle(this); } public void run() { loadingDiscView.update(); removeCallbacksAndMessages(null); sendEmptyMessageDelayed(0, 65); } public void stop() { removeCallbacksAndMessages(null); } @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: run(); break; } } }
step4、应用布局文件 res/layout/loading.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#382517" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/loading_disc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/loading_disc" android:paddingLeft="100dp" > </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="380dip" > <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:singleLine="true" android:textColor="#FFFFFF" android:text="读碟中,请稍后 . . ." android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
step5、写一个Activity用来装载布局文件,并展示 com.oyp.loadingdisk.LoadingActivity.java
package com.oyp.loadingdisk; import android.app.Activity; import android.os.Bundle; import android.widget.RelativeLayout; /** * @author ouyangpeng * @link http://blog.csdn.net/ouyang_peng */ public class LoadingActivity extends Activity { private RelativeLayout motionView; private LoadingDiscView disc_motion; private RefreshHandle refreshHandle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading); disc_motion = new LoadingDiscView(this); refreshHandle = new RefreshHandle(disc_motion); motionView = (RelativeLayout) findViewById(R.id.loading_disc); motionView.addView(disc_motion); refreshHandle.sendEmptyMessage(0); } @Override protected void onResume() { super.onResume(); disc_motion.onResume(); } }
以上就是怎么在Android利用Matrix对图片进行旋转,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。