自定义动画控件 AnimationView
来源:互联网 发布:猎鹿帽配什么衣服 知乎 编辑:程序博客网 时间:2024/06/05 18:55
AnimationView 继承View
public class AnimationView extends View { Bitmap[] bitmap; //图片的数组 int sleepTime = 1000; public boolean isRunning = true; int currentImageIndex = 0; int viewWidth, viewHeight; Thread thread; public AnimationView(Context context, AttributeSet attrs) { super(context, attrs); //读取自定义属性 typedArray 封装了子控件的属性 //attrs布局文件中定义的所有属性 TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.AnimationView); float defValue=1000; sleepTime=(int) typedArray.getFloat(R.styleable.AnimationView_sleep_time, defValue); //读取数组 Resources resources=context.getResources(); //获取数组 TypedArray taImage=resources.obtainTypedArray(R.array.animationImages); int length=taImage.length(); bitmap = new Bitmap[length]; for (int i=0;i<length;i++) { //读取图的id int imageId=taImage.getResourceId(i, 0); Bitmap image=BitmapFactory.decodeResource(resources, imageId); bitmap[i]=image; Log.i("测量", "image:"+image.getWidth()+","+image.getHeight()); } MyRunnable myRunnable = new MyRunnable(); thread = new Thread(myRunnable); thread.start(); } //测量控件大小,设置控件大小 //不测量,控件大小是父容器的大小,别的控件不显示 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //控件大小是图的大小. int imageWidth=bitmap[0].getWidth(); int imageHeight=bitmap[0].getHeight(); setMeasuredDimension(imageWidth, imageHeight); } @Override protected void onDraw(Canvas canvas) { Bitmap drawImage = bitmap[currentImageIndex]; int x = (viewWidth - drawImage.getWidth()) / 2; int y = (viewHeight - drawImage.getHeight()) / 2; Paint paint = new Paint(); canvas.drawBitmap(drawImage, x, y, paint); Log.i("animationView", "onDraw x=" + x); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub super.onSizeChanged(w, h, oldw, oldh); viewHeight = h; viewWidth = w; } class MyRunnable implements Runnable { @Override public void run() { while (isRunning) { try { currentImageIndex++; if (currentImageIndex >= bitmap.length) { currentImageIndex = 0; } // 工作线程中更新界面调postInvalidate(); // 主线程中更新界面调invalidate(); postInvalidate(); // invalidate(); Log.i("animationView", "currentImageIndex="+ currentImageIndex); Thread.currentThread().sleep(sleepTime); } catch (Exception e) { // TODO: handle exception } } } }}
二 资源文件 res—>values—>attrs.xml
<declare-styleable name="AnimationView"> <attr name="sleep_time" format="float"></attr> <attr name="images" format="reference"></attr> </declare-styleable>
三,布局文件 layout
<com.ttttttt.all.widget.AnimationView android:id="@+id/animationView" android:layout_width="wrap_content" android:layout_height="wrap_content" AnimationView:images="@array/animationImages" AnimationView:sleep_time="100" />
0 0
- 自定义动画控件 AnimationView
- 自定义控件 AnimationView 动画View
- 多种动画效果AnimationView
- Unity3D AnimationView动画视图、动画事件
- 自定义控件动画TextView
- 自定义控件 播放GIF动画
- 自定义控件——动画
- 自定义控件之帧动画
- 自定义控件之飘落动画
- 自定义控件-自定义动画的下来刷新
- Android自定义控件:动画类(4)-----自定义补间动画
- Dialog自定义布局上面,控件实现动画
- 自定义简单的逐帧动画控件
- 【自定义简单的逐帧动画控件】
- 【android自定义控件】属性动画 <五>
- 一种带动画自定义控件的封装
- 一种带动画自定义控件方法
- 自定义控件--带动画的CheckBox
- SystemService
- Linux下的旋钮驱动
- Android实现手势控制
- HDU3336 Count the string
- 关于BitMap中getWidth()与系统如何加载Drawable资源的问题
- 自定义动画控件 AnimationView
- Java之------类反射系列(二)
- 单向链表结点删除问题
- if top实时监控参考
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- Win10不能启动WampServer图标呈橘黄色的解决方法
- 简单认识JQuery的Ajax中的ajax()方法
- Happy Birthday To Myself
- [Python]Python 经典例题