Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程
今天實現(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)然,這里只是模擬碟片加載過程,實際上可以對代碼進(jìn)行處理,使碟片加載過程完畢后,啟動相應(yīng)的界面來展示碟片中的視頻、圖像、音樂資源等,但是這里不便寫出來。
關(guān)于源代碼,您可以通過 https://github.com/ouyangpeng/LoadingDisk 來免費察看和下載代碼。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin 集合函數(shù)map 和 first 的使用場景分析
Kotlin 提供了許多強大的集合操作函數(shù),其中 map 適用于轉(zhuǎn)換集合,first 適用于獲取符合條件的第一個元素,這篇文章給大家介紹Kotlin 集合函數(shù):map 和 first 的使用場景,感興趣的朋友一起看看吧2025-04-04
Android的Launcher啟動器中添加快捷方式及小部件實例
這篇文章主要介紹了在Android的Launcher啟動器中添加快捷方式及窗口小部件的方法,包括在自己的應(yīng)用程序中添加窗口小部件AppWidget的例子,需要的朋友可以參考下2016-02-02
Android5.x中的陰影效果elevation和translationZ的實現(xiàn)方法
這篇文章主要介紹了 android5.x中的陰影效果elevation和translationZ的相關(guān)資料,需要的朋友可以參考下2016-12-12
android studio開發(fā)實現(xiàn)APP開機自啟動
這篇文章主要為大家詳細(xì)介紹了android studio開發(fā)實現(xiàn)APP開機自啟動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android layoutAnimation詳解及應(yīng)用
這篇文章主要介紹了Android layoutAnimation詳解及應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-05-05
關(guān)于Android Studio封裝SDK的那些事兒
這篇文章主要給大家介紹了關(guān)于Android Studio封裝SDK的那些事兒,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

