gpt4 book ai didi

android - 在 ImageView 中查看类

转载 作者:太空狗 更新时间:2023-10-29 12:55:53 24 4
gpt4 key购买 nike

我开发了一个应用程序,我想在其中播放 gif 动画。为此,我引用了this

代码

public class GIFDemo extends GraphicsActivity {
ImageView imageView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
imageView1 = (ImageView) findViewById(R.id.imageView1);



}
private static class GIFView extends View{

Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
long moviestart1;
public GIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.border_gif);

movie=Movie.decodeStream(is);

}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();

if (moviestart == 0) { // first time
moviestart = now;

}
if(moviestart1==0)
{
moviestart1=now;
}

int relTime = (int)((now - moviestart) % movie.duration()) ;
// int relTime1=(int)((now - moviestart1)% movie1.duration());

movie.setTime(relTime);
// movie1.setTime(relTime1);
movie.draw(canvas,10,10);
//movie1.draw(canvas,10,100);
this.invalidate();
}
}
}

主要内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:src="@drawable/icon"></ImageView>
</LinearLayout>

所以问题是我想在 Imageview 中设置类 GIFView 所以我有像 this 这样的引用帖子但没有得到适当的例子。所以你能给我适当的例子和解释吗我如何在 ImageView 中设置 View

谢谢
尼克

最佳答案

我认为“设置 View ”ImageView 没有意义(按照您建议的方式),我认为您甚至不需要 ImageView 在这种情况下。您已经实现了自定义 View,它能够在不依赖于 ImageView 类的情况下绘制 GIF - 您只需要使用它即可!

通过最小的更改,假设 GIFView 工作,您可以实例化一个 GIFView,然后将它添加到主布局。让你的 onCreate 如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// let your LinearLayout have an id of 'container'
LinearLayout container = (LinearLayout) findViewById(R.id.container);
GIFView gifView = new GifView(this);
container.addView(gifView);

}

您可以完全摆脱布局中的 ImageView,只使用在代码中实例化的 GIFView。或者,您可以使您的 GIFView 成为一个独立的(非内部)类,并通过指定完整的包名称将其添加到您的 XML 布局中,例如

<com.nik.view.GIFView
id="@+id/gifview"
... />

您还需要编写 View 构造函数 GIFView(Context context, AttributeSet attrs),因为这是使用 XML 时所期望的构造函数。

关于android - 在 ImageView 中查看类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6881593/

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