自定义控件,实现加载图片时透明变换

自定义控件,一般都是通过继承已有的view类,如TextView,Button。。。或者直接继承其父类View。这次因为要加载图片。所以继承自ImageView。并且实现了一个通过属性来控制的加载时间。首先是自定义的类


    
  1. /**
  2. *知我者为我心忧,不知我者谓我何求!
  3. *linwoain@outlook.com
  4. *作者 linwoain
  5. *日期 2014/11/7 9:17
  6. */
  7. package com.linwoain.TestAndroid.fragment;
  8. import android.content.Context;
  9. import android.content.res.TypedArray;
  10. import android.graphics.Canvas;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.util.AttributeSet;
  14. import android.view.View;
  15. import android.widget.ImageView;
  16. import com.linwoain.TestAndroid.R;
  17. import java.util.Timer;
  18. import java.util.TimerTask;
  19. /**
  20. * 自定义控件,实现
  21. * @author linwoain
  22. * @version 2014/11/7 9:17
  23. */
  24. public class AlphaImageView extends ImageView {
  25. private static final int SPEED = 300;//每隔多少毫秒透明度改变一次
  26. private int alphaDelta = 0;//图像透明度每次改变的大小
  27. //记录图片当前的透明度
  28. private int curAlpha = 0;
  29. Handler handler = new Handler() {
  30. @Override
  31. public void handleMessage(Message msg) {
  32. super.handleMessage(msg);
  33. if (msg.what == 0x123) {
  34. curAlpha += alphaDelta;
  35. if (curAlpha > 255) {
  36. curAlpha = 255;
  37. AlphaImageView.this.setAlpha(curAlpha);
  38. }
  39. }
  40. }
  41. };
  42. public AlphaImageView(Context context) {
  43. this(context, null);
  44. }
  45. public AlphaImageView(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlphaImageView);
  48. int duration = typedArray.getInt(R.styleable.AlphaImageView_duration, 0);//默认不透明
  49. alphaDelta = 255 * SPEED / duration;
  50. typedArray.recycle();
  51. }
  52. @Override
  53. protected void onDraw(Canvas canvas) {
  54. this.setAlpha(curAlpha);
  55. super.onDraw(canvas);
  56. final Timer timer = new Timer();
  57. timer.schedule(new TimerTask() {
  58. @Override
  59. public void run() {
  60. Message message = new Message();
  61. message.what = 0x123;
  62. if (curAlpha >= 255) {
  63. timer.cancel();
  64. } else {
  65. handler.sendMessage(message);
  66. }
  67. }
  68. }, 0, SPEED);
  69. }
  70. }

这当中使用了属性AlphaImageView_duration,需要在values目录下新建attrs文件中声明,两种方式
1、不声明类型:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <attr name="duration"></attr>
  4. <declare-styleable name="AlphaImageView">
  5. <attr name="duration"></attr>
  6. </declare-styleable>
  7. </resources>

2、声明类型:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="AlphaImageView">
  4. <attr name="duration" format="integer"></attr>
  5. </declare-styleable>
  6. </resources>


第一种方式,有可能导致编译正确,但运行错误,第二种编译器会拒绝输入错误类型 。其中的AlphaImageView可以是非自定义控件的类名。然后在布局文件中添加一个AlphaImageView实例

  1. <com.linwoain.TestAndroid.fragment.AlphaImageView
  2. app:duration="6000"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/gif"/>

注意,在布局文件中使用自定义控件的属性时,需要引入命名控件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res/com.linwoain.TestAndroid"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
如上方所示,①的命名控件为安卓系统提供的控件的命名控件,②是自定义的命名控件,由http://schemas.android.com/apk/res+应用的包名 共同构成!

此时就完成了一个自定义控件!!











posted @ 2014-11-07 10:42  linwoain  阅读(308)  评论(0编辑  收藏  举报