我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
来源:互联网 发布:阿里云 ssh 编辑:程序博客网 时间:2024/04/28 09:54
今天实现了一个模拟碟片加载过程的小demo,在此展示一下。由于在公司,不好截取动态图片,因此就在这截取两张静态图片看看效果先。
下面简单的将代码列出来。
setp1、准备两张用于旋转的图片,如下:loading_disc.png是第一张图片,loading_light.png是第二张图片。
(1) (2)
step2、自定义一个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);}@Overridepublic 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;@Overrideprotected 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);}@Overrideprotected void onResume() {super.onResume();disc_motion.onResume();}}
当然,这里只是模拟碟片加载过程,实际上可以对代码进行处理,使碟片加载过程完毕后,启动相应的界面来展示碟片中的视频、图像、音乐资源等,但是这里不便写出来。
关于源代码,您可以通过 https://github.com/ouyangpeng/LoadingDisk 来免费察看和下载代码
====================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
===================================================================================
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
- 我的Android进阶之旅------>Android中通过adb shell input来模拟滑动、按键、点击事件
- 我的Android进阶之旅------>Android中通过adb shell input来模拟滑动、按键、点击事件
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>Android图片处理(Matrix,ColorMatrix)
- 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条
- 运用android的Matrix类来旋转图片
- Android 通过Matrix来对图片进行缩放,旋转和平移
- 我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例
- Android之matrix类控制图片的旋转、缩放、移动
- android使用matrix控制图片的旋转,缩放
- 我的Android进阶之旅------>Android使用9Patch图片作为不失真背景
- 我的Android进阶之旅------>Android使用9Patch图片作为不失真背景
- 我的Android进阶之旅------>关于使用CSDN-markdown编辑器来编写博客
- Effective Objective-C 2.0: Item 17: Implement the description Method
- 14-7体验文件的操作
- Spring学习资料
- LINUX字符设备驱动程序实例(scull)
- MySQL索引优化
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
- 实验十一 连接数据库实验(V2.0)
- 将 Linux 应用程序移植到 64 位系统上
- 影响一生的职业建议
- 简单的javac引起的错误
- apache和tomcat区别
- TOJ 1772
- 线性表插入删除笔记
- WindowManager.LayoutParams//android.view