音乐喷泉播放效果探究

方法一、直接播放Gif图片,但是在魅族手机上无法执行

1 自定义View

public class SongEffectView extends View {
    private long movieStart;
    private Movie movie;
    // 此处必须重写该构造方法
    public SongEffectView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // 以文件流(InputStream)读取进gif图片资源
        movie = Movie.decodeStream(getResources().openRawResource(
                R.drawable.a11232));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        long curTime = android.os.SystemClock.uptimeMillis();
        // 第一次播放
        if (movieStart == 0) {
            movieStart = curTime;
        }
        if (movie != null) {
            int duraction = movie.duration();
            int relTime = (int) ((curTime - movieStart) % duraction);
            movie.setTime(relTime);
            movie.draw(canvas, 0, 0);
            // 强制重绘
            invalidate();
        }
        super.onDraw(canvas);
    }
}

2 布局文件中引用

        <com.rednovo.weibo.widget.main.page.SongEffectView
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:layout_gravity="center"
        android:visibility="visible"
        />

方法二、播放打散的Gif图片

1 打散gif图片,成为一帧一帧的部分

2 在anim文件下新建文件

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >

<item
android:drawable="@drawable/song01"
android:duration="150"/>
<item
android:drawable="@drawable/song02"
android:duration="150"/>
</animation-list>

3 布局文件中添加

 <ImageView
         android:id="@+id/id_img_song_effect"
         android:layout_width="62dp"
         android:layout_height="62dp"
         android:layout_gravity="bottom"
         android:background="@anim/song_effect"/>   

4 代码控制动起来

    ImageView songEffect = (ImageView) findViewById(R.id.id_img_song_effect);
    AnimationDrawable anim = null;  
    Object ob = songEffect.getBackground();  
    anim = (AnimationDrawable) ob;  
    anim.stop();  
    anim.start(); 

8/10/2015 3:16:12 PM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值