Android Drawable Resource学习(七)、TransitionDrawable

一个TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果。

<transition>节点下的每个<item>代表一个drawable资源。只能有两个<item>。先前转换调用startTransition()。向后,调用 reverseTransition()

文件位于:
res/drawable/filename.xml
文件名作为资源ID
编译资源类型:
指向 TransitionDrawable的指针
资源引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
语法:
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition>
元素:
<transition>
必须的。  必须作为根节点,包含一个或多个<item>元素。

属性:

xmlns:android
字符串类型, 必须的。定义XML文件的命名空间,必须是 "http://schemas.android.com/apk/res/android".
<item>
定义一个TransitionDrawable中所使用的一个drawable。必须是 <transition>子节点。可以接受 <bitmap>子节点。

属性:

android:drawable
Drawable 资源。 必须的。引用一个Drawable资源。
android:id
资源ID。drawable资源的唯一标识。 使用"@+id/name"方式来给这个item定义一个新的资源ID。可以使用 View.findViewById() 或者 Activity.findViewById()等方式检索和修改这个item。
android:top
Integer。 与顶部的距离
android:right
Integer。与右边的距离
android:bottom
Integer。 与下边的距离
android:left
Integer。与左边的距离
例子:
XML文件保存为: res/drawable/transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>

在layout文件中使用:

<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />

And the following code performs a 500ms transition from the first item to the second:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);
参考
  • TransitionDrawable


下面是实例:

1、xml方式使用

transition.xml:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/image01"/>
    <item android:drawable="@drawable/image02"/>

</transition>

在layout中使用:

    <ImageView 
        android:id="@+id/imgView"
        android:src="@drawable/transition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

在代码中控制:

ImageView imageView = (ImageView) findViewById(R.id.imgView);
        TransitionDrawable transitionDrawable = (TransitionDrawable) imageView.getDrawable();
        transitionDrawable.startTransition(3000);




下面是一个实例:实现多张图片循环的淡入淡出的效果。

package com.example.drawabletest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private int change=0;
    private int[] ids = new int[] { R.drawable.image1, R.drawable.image2, R.drawable.image3,
            R.drawable.image4, R.drawable.image5 };
    private Drawable[] drawables;
    private ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imgView);

        /*获得合适的drawable资源*/
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.image1, opts);
        opts.inSampleSize = computeSampleSize(opts, -1, 500 * 500);
        opts.inJustDecodeBounds = false;
        drawables=new Drawable[ids.length];
        try {
            for (int i = 0; i < ids.length; i++) {// for循环,加载5个drawable资源
                Bitmap bmp = BitmapFactory.decodeResource(getResources(), ids[i], opts);
                drawables[i] = new BitmapDrawable(bmp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //开启线程,改变transition
        new Thread(new MyRunnable()).start();
        
    }

    //处理transition的改变
    private Handler handler=new Handler(new Handler.Callback() {
        public boolean handleMessage(Message msg) {
            int duration=msg.arg1;
            TransitionDrawable transitionDrawable=null;
            transitionDrawable= new TransitionDrawable(new Drawable[] {
                        drawables[change%ids.length],//实现从0 1 2 3 4 5 0 1 2.。。这样的不停转变
                        drawables[(change+1)%ids.length] });
            change++;
            imageView.setImageDrawable(transitionDrawable);
            transitionDrawable.startTransition(duration);
            return false;
        }
    });
    
    //线程,去发送消息,让transition一直改变
    private class MyRunnable implements Runnable{
        public void run() {
            while (true) {
                int duration=3000;//改变的间隔
                Message message=handler.obtainMessage();
                message.arg1=duration;
                handler.sendMessage(message);
                try {
                    Thread.sleep(duration);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    //计算合适的图片大小
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength,
            int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    //计算合适的图片大小
    private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
            int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h
                / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}



 




  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值