Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程
今天實(shí)現(xiàn)了一個模擬碟片加載過程的小demo,在此展示一下。由于在公司,不好截取動態(tài)圖片,因此就在這截取兩張靜態(tài)圖片看看效果先。
下面簡單的將代碼列出來。
setp1、準(zhǔn)備兩張用于旋轉(zhuǎn)的圖片,如下:loading_disc.png是第一張圖片,loading_light.png是第二張圖片。
step2、自定義一個View,用來控制這兩個圖片的旋轉(zhuǎn)。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>在畫圖的時候,圖片如果旋轉(zhuǎn)或縮放之后,總是會出現(xiàn)那些華麗的鋸齒。<br> * 方法一:給Paint加上抗鋸齒標(biāo)志。然后將Paint對象作為參數(shù)傳給canvas的繪制方法。<br> * 如:mypaint.setAntiAlias(true);<p> * 方法二:給Canvas加上抗鋸齒標(biāo)志。有些地方不能用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; /** 用于旋轉(zhuǎn)的bitmap*/ private Bitmap m_bmp_disc = null; private Matrix m_matrix_disc = new Matrix(); /** 用于展現(xiàn)高亮背景的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; /**圖像旋轉(zhuǎn)的速度*/ private float m_disc_rot_speed = 0; /**圖像旋轉(zhuǎn)的狀態(tài)*/ private int m_state_play = 1; /**圖像旋轉(zhuǎn)的最大速度*/ 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);//設(shè)置畫布繪圖無鋸齒 initBitmap(); } public boolean initBitmap() { mypaint = new Paint(); //給Paint加上抗鋸齒標(biāo)志 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; } /** * 旋轉(zhuǎn)圖像 * @param matrix 控制旋轉(zhuǎn)的矩陣 * @param bitmap 要旋轉(zhuǎn)的圖像 */ 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); //設(shè)置平移位置 matrix.postScale(m_scale, m_scale); //設(shè)置縮放比例 matrix.postTranslate(123 * m_scale, 146 * m_scale); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); //給Canvas加上抗鋸齒標(biāo)志 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用來控制圖片的旋轉(zhuǎn) com.oyp.loadingdisk.RefreshHandle.java
package com.oyp.loadingdisk; import android.os.Handler; import android.os.Message; /** * 用來發(fā)送消息和處理消息的 * @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、應(yīng)用布局文件 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(); } }
當(dāng)然,這里只是模擬碟片加載過程,實(shí)際上可以對代碼進(jìn)行處理,使碟片加載過程完畢后,啟動相應(yīng)的界面來展示碟片中的視頻、圖像、音樂資源等,但是這里不便寫出來。
關(guān)于源代碼,您可以通過 https://github.com/ouyangpeng/LoadingDisk 來免費(fèi)察看和下載代碼。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android單點(diǎn)觸控實(shí)現(xiàn)圖片平移、縮放、旋轉(zhuǎn)功能
這篇文章主要介紹了Android單點(diǎn)觸控實(shí)現(xiàn)圖片平移、縮放、旋轉(zhuǎn)功能的相關(guān)資料,需要的朋友可以參考下2016-02-02Android系列之Intent傳遞對象的幾種實(shí)例方法
Android系列之Intent傳遞對象的幾種實(shí)例方法,需要的朋友可以參考一下2013-05-05Android中WebView的使用與后退鍵處理詳細(xì)講解
博主自從開始寫安卓以來,一直飽受WebView的摧殘,好在網(wǎng)上一大堆的大神給出了他們成長路上遇到的坑以及一些解決辦法,這篇文章主要給大家介紹了關(guān)于Android中WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04Android 開發(fā)中fragment預(yù)加載問題
這篇文章主要介紹了Android 開發(fā)中fragment預(yù)加載問題的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01android6.0運(yùn)行時權(quán)限完美封裝方法
今天小編就為大家分享一篇android6.0運(yùn)行時權(quán)限完美封裝方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android仿支付寶笑臉?biāo)⑿录虞d動畫的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿支付寶笑臉?biāo)⑿录虞d動畫的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android利用ViewPager實(shí)現(xiàn)滑動廣告板實(shí)例源碼
利用ViewPager我們可以做很多事情,從最簡單的導(dǎo)航,到頁面切換菜單等等。ViewPager的功能就是可以使視圖滑動,就像Lanucher左右滑動那樣2013-06-06Android實(shí)現(xiàn)手機(jī)振動設(shè)置的方法
這篇文章主要介紹了Android實(shí)現(xiàn)手機(jī)振動設(shè)置的方法,涉及Android頁面布局、屬性及功能設(shè)置的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09