我们掩盖文字动画Flash中非经货共同体共同,由于Android应用程序开发人员做你想要做这个动画在应用程序中去?本文中,我们看的是如何自己的定义ImageView来实现让一张文字图片实现文字的遮罩闪烁效果,以下先来看看效果吧。

(录屏幕延时导致效果看起来不是非常好)

一、实现原理

  实现原理是重写View的onCreate方法。获取图片资源后对每一个像素的透明度进行改动来实现,再启动一个线程来循环改变某个区域中的像素透明度。

RGBA基础知识:(以下几段介绍文字引用自维基百科)

  RGBA是代表Red(红色)Green(绿色)Blue(蓝色)和Alpha的色彩空间。尽管它有的时候被描写叙述为一个颜色空间,可是它事实上不过RGB模型的附加了额外的信息。

採用的颜色是RGB,能够属于不论什么一种RGB颜色空间。可是

title=Edwin_Catmull&action=edit&redlink=1" class="new" title="Edwin Catmull(页面不存在)" style="background-image: none;">CatmullSmith在1971至1972年间提出了这个不可或缺的alpha数值,使得

title=Alpha%E6%B8%B2%E6%9F%93&action=edit&redlink=1" class="new" title="Alpha渲染(页面不存在)" style="background-image: none;">alpha渲染

title=Alpha%E5%90%88%E6%88%90&action=edit&redlink=1" class="new" title="Alpha合成(页面不存在)" style="background-image: none;">alpha合成变得可能。

提出者以alpha来命名是源于经典的线性插值方程αA + (1-α)B所用的就是这个希腊字母

  alpha通道一般用作不透明度參数。假设一个像素的alpha通道数值为0%,那它就是全然透明的(也就是看不见的),而数值为100%则意味着一个全然不透明的像素(传统的数字图像)。在0%和100%之间的值则使得像素能够透过背景显示出来,就像透过玻璃(半透明性),这样的效果是简单的二元透明性(透明或不透明)做不到的。它使数码合成变得easy。alpha通道值能够用百分比、整数或者像RGB參数那样用0到1的实数表示。

  有时它也被写成ARGB(像RGBA一样,可是第一个数据是alpha),是Macromedia的产品使用的术语。比方,0x80FFFF00是50%透明的黄色,由于全部的參数都在0到255的范围内表示。0x80是128,大约是255的一半。

PNG是一种使用RGBA的图像格式。

二、具体实现

package com.example.helloworld;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
/**
* @author 阳光小强
*
*/
public class SplashImageView extends ImageView{
private Bitmap alterBitmap;
private Canvas canvas;
private Paint paint;
private Handler handler;
private static int START_POSITION = 20;
private final int speed;
private int nowPosition = START_POSITION;
private static int SHOW_WIDTH = 20;
private boolean isFirst = true;
private boolean isStop = false; private class MyHandler extends Handler { private static final long SCALE = 10000;
private static final int MSG_PAINT = 1; private final SplashImageView owner;
private final int speed; private long angle;
private long lastTime;
public MyHandler(SplashImageView owner) {
this.owner = owner;
this.lastTime = SystemClock.elapsedRealtime();
this.speed = owner.speed;
sendEmptyMessage(MSG_PAINT);
} @Override
public void handleMessage(Message msg) {
if (msg.what == MSG_PAINT) {
long now = SystemClock.elapsedRealtime();
long delta_time = now - lastTime;
System.out.println("delta_time = " + delta_time);
System.out.println("alterBitmap.Width = " + alterBitmap.getWidth());
if(nowPosition + speed >= alterBitmap.getWidth() - START_POSITION - SHOW_WIDTH){
if(isStop){
handler.removeCallbacksAndMessages(null);
handler = null;
isStop = false;
return;
}else{
nowPosition = START_POSITION;
}
}
nowPosition = nowPosition + speed;
if (delta_time > 0) {
if(!notifiDraw(nowPosition)){
return;
}
}
this.sendEmptyMessageDelayed(MSG_PAINT, 10);
}
}
} private boolean notifiDraw(long position) {
System.out.println("nofityDrawToatal = " + position);
if(position < alterBitmap.getWidth() - START_POSITION - SHOW_WIDTH){
this.invalidate();
return true;
}
if (handler != null) {
handler.removeCallbacksAndMessages(null);
handler = null;
}
return false;
} @Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if(visibility == View.VISIBLE){
if(handler == null){
handler = new MyHandler(this);
}else{
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessage(MyHandler.MSG_PAINT);
}
}else{
if(handler != null){
handler.removeCallbacksAndMessages(null);
handler = null;
}
}
} public void stopSplashAnimation(){
if(handler != null){
isStop = true;
}
} public SplashImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FuseImageView, 0, 0);
int resId = a.getResourceId(R.styleable.FuseImageView_imageSrc, 0);
int speed = a.getInt(R.styleable.FuseImageView_speed, 5);
this.speed = speed <= 0 ? 1 : speed;
Bitmap up = BitmapFactory.decodeResource(context.getResources(), resId);
alterBitmap = Bitmap.createBitmap(up.getWidth(), up.getHeight(), up.getConfig()); canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLACK);
canvas.drawBitmap(up, new Matrix(), paint); setImageBitmap(alterBitmap); if(getVisibility() == View.VISIBLE){
if(handler == null){
handler = new MyHandler(this);
}
}
} @Override
protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(isFirst){
isFirst = false;
for(int i=nowPosition; i<alterBitmap.getWidth() ; i++){
for(int j=0; j<alterBitmap.getHeight(); j++){
int color = alterBitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
if( a > 200){
color = Color.argb(80, r, g, b);
}else{
color = Color.argb(a, r, g, b);
}
alterBitmap.setPixel(i, j, color);
} }
} for(int i=nowPosition; i<nowPosition + SHOW_WIDTH ; i++){
for(int j=0; j<alterBitmap.getHeight(); j++){
int color = alterBitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
if(a == 80){
color = Color.argb(255, r, g, b);
}else{
color = Color.argb(a, r, g, b);
}
alterBitmap.setPixel(i, j, color);
} } if(nowPosition > START_POSITION){
for(int i= nowPosition - SHOW_WIDTH; i<nowPosition; i++){
for(int j=0; j<alterBitmap.getHeight(); j++){
int color = alterBitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
if( a > 200){
color = Color.argb(80, r, g, b);
}else{
color = Color.argb(a, r, g, b);
}
alterBitmap.setPixel(i, j, color);
} }
}
setImageBitmap(alterBitmap);
} }

三、实现具体解释

1、构造方法中进行初始化操作

	public SplashImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FuseImageView, 0, 0);
int resId = a.getResourceId(R.styleable.FuseImageView_imageSrc, 0);
int speed = a.getInt(R.styleable.FuseImageView_speed, 5);
this.speed = speed <= 0 ? 1 : speed;
Bitmap up = BitmapFactory.decodeResource(context.getResources(), resId);
alterBitmap = Bitmap.createBitmap(up.getWidth(), up.getHeight(),
up.getConfig()); canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLACK);
canvas.drawBitmap(up, new Matrix(), paint); setImageBitmap(alterBitmap); if (getVisibility() == View.VISIBLE) {
if (handler == null) {
handler = new MyHandler(this);
}
}
}

上面的TypedArray是自己定义的属性,在res/values文件夹下新建一个attrs.xml加入自己定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FuseImageView">
<attr name="imageSrc" format="reference" />
<attr name="speed" format="integer" />
</declare-styleable>
</resources>

这里是自己定义的两个属性,一个是图片资源ID还有一个是遮罩移动速度(事实上上面也能够继承自View来实现自己定义。我这里是有特殊须要才继承自ImageView的)

然后通过BitmapFactory获取图片资源。并通过createBitmap方法创建一个可写的Bitmap资源给画布(Canvas)。将可写的Bitmap绘制到相同资源的背景上。

底下的判读View是否可看见,是用来判读View是否可见,假设可见才开启线程进行动画的。不然的话开启线程绘制会浪费资源的(由于它根本就看不见)。

2、怎样改变透明度而且绘制(onDraw方法)

		for (int i = nowPosition; i < nowPosition + SHOW_WIDTH; i++) {
for (int j = 0; j < alterBitmap.getHeight(); j++) {
int color = alterBitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
if (a == 80) {
color = Color.argb(255, r, g, b);
} else {
color = Color.argb(a, r, g, b);
}
alterBitmap.setPixel(i, j, color);
} } if (nowPosition > START_POSITION) {
for (int i = nowPosition - SHOW_WIDTH; i < nowPosition; i++) {
for (int j = 0; j < alterBitmap.getHeight(); j++) {
int color = alterBitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
if (a > 200) {
color = Color.argb(80, r, g, b);
} else {
color = Color.argb(a, r, g, b);
}
alterBitmap.setPixel(i, j, color);
} }
}

主要是上面两个循环来实现绘制的,上面的循环是绘制一块区域来将文字的透明度调为最小(255),这一部分的文字就显示为高亮了,其余部分的文字透明度调值调为80,就会显示背景颜色,文字的暗度就会下降。

3、怎样循环移动遮罩

	private class MyHandler extends Handler {

		private static final long SCALE = 10000;
private static final int MSG_PAINT = 1; private final SplashImageView owner;
private final int speed; private long angle;
private long lastTime; public MyHandler(SplashImageView owner) {
this.owner = owner;
this.lastTime = SystemClock.elapsedRealtime();
this.speed = owner.speed;
sendEmptyMessage(MSG_PAINT);
} @Override
public void handleMessage(Message msg) {
if (msg.what == MSG_PAINT) {
long now = SystemClock.elapsedRealtime();
long delta_time = now - lastTime;
System.out.println("delta_time = " + delta_time);
System.out.println("alterBitmap.Width = "
+ alterBitmap.getWidth());
if (nowPosition + speed >= alterBitmap.getWidth()
- START_POSITION - SHOW_WIDTH) {
if (isStop) {
handler.removeCallbacksAndMessages(null);
handler = null;
isStop = false;
return;
} else {
nowPosition = START_POSITION;
}
}
nowPosition = nowPosition + speed;
if (delta_time > 0) {
if (!notifiDraw(nowPosition)) {
return;
}
}
this.sendEmptyMessageDelayed(MSG_PAINT, 10);
}
}
}

循环移动遮罩是写在一个线程中的。每隔10毫秒就去移动speed(配置的速度)的距离。来实现遮罩的移动效果,再取图片的宽度来推断是否已经到了最右边。


总结:事实上上面的实现原理并不难。要点是要知道RGBA的知识和怎样去改变像素的透明度。这个不过个人临时想到的一个方法,假设有什么更好的方式实现。希望能交流一下。

另外“阳光小强”的还有一篇博文《是男人就下100层【第三层】——高仿交通银行手机client界面》參加了CSDN举办的博文大赛。假设您认为这些博文对您有帮助。希望您投出您宝贵的一票。投票地址:http://vote.blog.csdn.net/Article/Details?articleid=30101091

版权声明:本文博主原创文章,博客,未经同意不得转载。

Android自己定义组件系列【8】——面膜文字动画的更多相关文章

  1. Android自己定义组件系列【7】——进阶实践(4)

    上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...

  2. Android自己定义组件系列【6】——进阶实践(3)

    上一篇<Android自己定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计 ...

  3. Android自己定义组件系列【5】——进阶实践(2)

    上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...

  4. Android自己定义组件系列【4】——自己定义ViewGroup实现双側滑动

    在上一篇文章<Android自己定义组件系列[3]--自己定义ViewGroup实现側滑>中实现了仿Facebook和人人网的側滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布 ...

  5. Android自己定义组件系列【3】——自己定义ViewGroup实现側滑

    有关自己定义ViewGroup的文章已经非常多了,我为什么写这篇文章,对于刚開始学习的人或者对自己定义组件比較生疏的朋友尽管能够拿来主义的用了,可是要一步一步的实现和了解当中的过程和原理才干真真脱离别 ...

  6. android 自己定义组件随着手指自己主动画圆

    首先自己定义一个View子类: package com.example.androidtest0.myView; import android.content.Context; import andr ...

  7. Android自己定义组件系列【9】——Canvas绘制折线图

    有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了非常多插件,可是非常多时候我们须要依据详细项目自己定义这些图表,这一篇文章我们一起来看看怎样在Android中使用Can ...

  8. Android自己定义组件系列【1】——自己定义View及ViewGroup

    View类是ViewGroup的父类,ViewGroup具有View的全部特性.ViewGroup主要用来充当View的容器.将当中的View作为自己孩子,并对其进行管理.当然孩子也能够是ViewGr ...

  9. Android自己定义组件系列【2】——Scroller类

    在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...

随机推荐

  1. Entity Framework的事务提交

    一组业务整体处理的行为叫一个事务.这一组的业务都能成功处理,我们就可以把这个事务提交来保存你已做的行为结果.事物的Commit是执行了你的方法进行了数据库的提交,之前的sava都是放在缓存中并没有执行 ...

  2. DS-5/RVDS4.0变量初始化错误

    最近总是有各种调试时的错误,内存错误一般,我无意中发现的所有全局变量失败,也就是说,全局变量声明之后,颂值早期值误. 初值明明是0x12345674,最后变为了0xBA141234,出现了移位现象 測 ...

  3. [LeetCode203]Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...

  4. vector和list删除元素

    #include <iostream> #include <vector> #include <list> using namespace std; int mai ...

  5. Unity判断网络连接类型

    使用NetworkReachability判断手机游戏当前的网络连接类型,是wifi还是234G using UnityEngine; using System.Collections; public ...

  6. 有一个NSStirng类型,retain时尚宣言name财产setter内部方法的每一行代码的作用?

    - (void)setName:(NSString *)name {        推断原有对象和新对象是否是同一个对象,假设是同一个,就没有必要再又一次赋值,否则会先release 再retain, ...

  7. AspUpload组件的安装及使用方法介绍

    http://soft.huweishen.com/soft/47.html AspUpload对ASP编程人员来说要实现ASP网站文件上传功能它是首选.本文就为大家介绍一下AspUpload组件的安 ...

  8. PHP redis操作类 个人总结

    <pre name="code" class="php"><span style="font-size:18px;"> ...

  9. 【我们都爱Paul Hegarty】斯坦福大学IOS8公开组个人笔记28 ScrollView 幻灯片视图

    随着移动设备,iphone屏幕尺寸的限制.超过内容的屏幕大小为scrollview于,通过滑动来获得.scrollview滑动方向可以是也可以是横向垂直,scrollview可以嵌套,例如,纵向滑动s ...

  10. Android 建立View 圆角

    虽然很easy,不过还是录制. 混合参观 在drawable文件下 创建一个布局文件corners_bg.xml <?xml version="1.0" encoding=& ...