使用Movie播放Gif图片

gif:实现屏幕自适应以及控制与监听播放时间

class MyCustomView extends View {
private Movie mMovie;
    private long mMovieStart;
    private int mWidth,mHeight;
    private int mViewWidht, mViewHeight;
    private OnPlayListener listener;
    private boolean isDraw = true;
    private String TAG=MyCustomView.class.getSimpleName();
    public MyCustomView(Context context) {
        super(context);
        init(context);
    }


    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}


public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
    private void init(Context context){
    // 以文件流的方式读取文件
        mMovie = Movie.decodeStream(getResources().openRawResource(
                R.drawable.a));
        WindowManager manager=((Activity)context).getWindowManager();
        DisplayMetrics outMetrics=new DisplayMetrics();
   manager.getDefaultDisplay().getMetrics(outMetrics);
   //屏幕宽高
   mHeight=outMetrics.heightPixels;
   mWidth=outMetrics.widthPixels;
  //gif图片宽度,高度
  mViewHeight = mMovie.height();
  mViewWidht = mMovie.width();
    }
    
public void setListener(OnPlayListener listener) {
this.listener = listener;
}


@Override
    protected void onDraw(Canvas canvas) {
        long curTime = android.os.SystemClock.uptimeMillis();
        if (isDraw) {
        // 第一次播放
        if (mMovieStart == 0) {
            mMovieStart = curTime;
        }


        if (mMovie != null) {
        int duration = mMovie.duration();
        if (duration==0) {
        duration = (int) TimeUnit.SECONDS.toSeconds(5000);
}
        //控制播放时间
              if (curTime-mMovieStart>duration) {
              isDraw = false;
                   if (listener != null) {
                  listener.onFinished();
                   }
}
            int relTime = (int) ((curTime - mMovieStart) % duration);
            mMovie.setTime(relTime);
          //计算缩放比例
            float saclex = (float) mWidth / (float) mViewWidht;
            float sacley = (float) mHeight / (float) mViewHeight;
            Log.e(TAG, "mWidth=="+mWidth+",mHeight=="+mHeight);
            Log.e(TAG, "mViewWidht=="+mViewWidht+",mViewHeight=="+mViewHeight);
            canvas.scale(saclex, sacley);
            mMovie.draw(canvas, 0, 0);
            postInvalidate();
         }
        }
    }
//关闭接口
public interface OnPlayListener {
   public void onFinished();
}
}

-----------------------------------------------------------------------------------------------------------

public class MainActivity extends Activity implements OnPlayListener{
  private MyCustomView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
myView=(MyCustomView) findViewById(R.id.myView);
myView.setListener(this);
}


@Override
public void onFinished() {
       Toast.makeText(getApplicationContext(), "is over", 1).show();
       finish();
}


}

-------------------------------------------------------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00ff00"
    tools:context=".MainActivity" >
<com.example.showMovie.MyCustomView
    android:id="@+id/myView"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    >
    
</com.example.showMovie.MyCustomView>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值