gpt4 book ai didi

java - 如何在 Android Studio 中设置全屏背景 gif

转载 作者:行者123 更新时间:2023-11-29 19:42:12 31 4
gpt4 key购买 nike

我的应用程序中有一张 GIF 图片,我希望它成为我的全屏背景。我使用 width and height = match parent 但它没有变成全屏。这是我的代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
GifView pGif = (GifView) findViewById(R.id.gif);
pGif.setImageResource(R.drawable.bbbb);
}}

这是我的课:

public class GifView extends View {


private static final int DEFAULT_MOVIEW_DURATION = 1000;

private int mMovieResourceId;
private Movie mMovie;

private long mMovieStart = 0;
private int mCurrentAnimationTime = 0;

@SuppressLint("NewApi")
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);

/**
* Starting from HONEYCOMB have to turn off HardWare acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}

public void setImageResource(int mvId){
this.mMovieResourceId = mvId;
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(mMovie != null){
setMeasuredDimension(mMovie.width(), mMovie.height());
}else{
setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
}
}

@Override
protected void onDraw(Canvas canvas) {
if (mMovie != null){
updateAnimtionTime();
drawGif(canvas);
invalidate();
}else{
drawGif(canvas);
}
}

private void updateAnimtionTime() {
long now = android.os.SystemClock.uptimeMillis();

if (mMovieStart == 0) {
mMovieStart = now;
}
int dur = mMovie.duration();
if (dur == 0) {
dur = DEFAULT_MOVIEW_DURATION;
}
mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}

private void drawGif(Canvas canvas) {
mMovie.setTime(mCurrentAnimationTime);
mMovie.draw(canvas, 0, 0);
canvas.restore();
}

}

和 xml:

<com.example.fabulous.comic.GifView
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gif"/>

最佳答案

您可以在 glide 的帮助下完成此操作

它是一个专注于平滑滚动的Android图像加载和缓存库

Glide 支持获取、解码和显示视频静止图像、图像和动画 GIF

你可以使用这个来使用 Glide

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />

<!--Rest of your coding-->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</FrameLayout>

Java

ImageView img=(ImageView)findViewById(R.id.img);

Glide.with(Gif.this).load(R.drawable.giphy).asGif().into(img);

ps: 我没有测试代码

关于java - 如何在 Android Studio 中设置全屏背景 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481121/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com