下面列出了怎么用android.graphics.Movie的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifView, defStyle, R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
private static MediaInfo mediaInfoFixup(MediaInfo info)
{
if(info.container != null && info.container == MediaContainer.GIF)
{
// ffprobe will not report the duration of GIF files. The duration will
// need to be determined another way
Movie gifMovie = Movie.decodeFile(info.file.getAbsolutePath());
int durationMs = gifMovie.duration();
info = new MediaInfo(info.file, durationMs, info.container, info.videoCodec, info.videoResolution,
info.videoBitrateK, info.videoFramerate, info.audioCodec, info.audioSampleRate, info.audioBitrateK, info.audioChannels);
}
return info;
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
public GifDrawable(Movie movie, int height, int width) {
this.movie = movie;
this.height = height;
this.width = width;
setBounds(0, 0, width, height);
scaleX = scaleY = 1.0f;
paint = new Paint();
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.what == what && running && textView != null) {
textView.invalidate();
sendEmptyMessageDelayed(what, 33);
}
}
};
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifView, defStyle, R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = false;
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
/**
* Return a movie object associated with the particular resource ID.
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
*
*/
public Movie getMovie(@RawRes int id) throws NotFoundException {
final InputStream is = openRawResource(id);
final Movie movie = Movie.decodeStream(is);
try {
is.close();
} catch (IOException e) {
// No one cares.
}
return movie;
}
private void init() {
setFocusable(true);
mMovie = Movie.decodeStream(mInputStream);
mWidth = mMovie.width();
mHeight = mMovie.height();
requestLayout();
}
@Override
public Movie getMovie(int id) throws NotFoundException {
try {
return super.getMovie(id);
} catch (NotFoundException e) {
return mHostResources.getMovie(id);
}
}
@Override
public Movie getMovie(int id) throws NotFoundException {
try {
return super.getMovie(id);
} catch (NotFoundException e) {
return mHostResources.getMovie(id);
}
}
public void setGifResource(int movieResourceId) {
/**
* Starting from HONEYCOMB(Api Level:11) have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
movie = Movie.decodeStream(getResources().openRawResource(movieResourceId));
requestLayout();
}
public void setGifResource(String filepath) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
InputStream inputStream = ResourceUtil.getAssetsStream(filepath);
if (inputStream != null) {
movie = Movie.decodeStream(inputStream);
requestLayout();
}
}
public void setMovieResource(InputStream inputStream) {
//this.mMovieResourceId = movieResId;
// mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
mMovie = Movie.decodeStream(inputStream);
requestLayout();
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView, defStyle, R.style.AppBaseTheme);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView, defStyle,
R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView, defStyle,
R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
@Override
public Movie getMovie(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getMovie(repId);
}
return super.getMovie(id);
}
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView, defStyle,
R.style.Widget_GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
mPaused = array.getBoolean(R.styleable.GifView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
}
}
@Override
protected void onPostExecute(Object[] result) {
super.onPostExecute(result);
ViewGroup v = (ViewGroup) result[0];
Object img = result[1];
if (img != null) {
v.removeAllViews();
if (img instanceof Bitmap) {
PhotoView p = new PhotoView(ImageActivity.this);
// Disable hardware acceleration if too large
Bitmap image = (Bitmap) img;
int maxSize = Utility.getSupportedMaxPictureSize();
if (image.getWidth() > maxSize || image.getHeight() > maxSize) {
p.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
if (DEBUG) {
Log.d(TAG, "Image too large, hardware acceleration disabled. max size: " + maxSize);
}
}
p.setImageBitmap(image);
p.setOnPhotoTapListener(ImageActivity.this);
v.addView(p, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else if (img instanceof Movie) {
GifView g = new GifView(ImageActivity.this);
g.setMovie((Movie) img);
v.addView(g, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif,
-1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
/**
* Starting from HONEYCOMB have to turn off HW acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifMoviewView, defStyle,
R.style.Widget_GifMoviewView);
mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif,
-1);
mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);
array.recycle();
if (mMovieResourceId != -1) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
public GIFView(Context context, @NonNull final byte[] data) {
super(context);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mMovie = Movie.decodeByteArray(data, 0, data.length);
if(mMovie.duration() < 1) {
throw new RuntimeException("Invalid GIF");
}
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
}
public void setMovieStream(InputStream in,int mf)
{
this.multiFrame = mf;
mMovie = Movie.decodeStream(in);
requestLayout();
}
public Movie getMovie() {
return mMovie;
}
public static Movie getMovie(@RawRes int rawRes) {
return Base.getResources().getMovie(rawRes);
}
public void setMovieResource(int movieResId) {
this.mMovieResourceId = movieResId;
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
requestLayout();
}