您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。