您的位置:首页 > 其它

使用Movie播放Gif图片

2015-01-14 15:57 323 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: