View class in ImageView
i have develop one application in which i want to play gif animation. for that i have refer this
CODE
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.o开发者_Python百科s.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();
}
}
}
Main :
<?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>
so problem is i want to set class GIFView
in Imageview
so i have refer post like this
but not get proper example. so can you give me proper example and explanation that how can i set view in ImageView
Thanks
nikI don't think it is meaningful to 'set the view' of an ImageView
(in the way you are suggesting), and I don't think you even need an ImageView
in this case. You have implemented a custom View
that is able to draw a GIF without any dependency on the ImageView
class - you just have to use it!
With minimal changes, assuming the GIFView
works, you can just instantiate a GIFView
and then add it to the main layout. Let your onCreate
be as follows:
@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);
}
You can get rid of the ImageView from the layout entirely and just use your GIFView
that was instantiated in code. Alternatively, you can make your GIFView
a standalone (non-inner) class and add it to your XML layout by specifying the full package name, e.g.
<com.nik.view.GIFView
id="@+id/gifview"
... />
You would also need to write the view constructor GIFView(Context context, AttributeSet attrs)
because that is the one that is expected when using XML.
精彩评论