2011-07-30 16 views
3

私はgifアニメーションを再生したいアプリケーションを1つ開発しました。 iがthisImageViewのクラスを表示

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.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> 

ので、問題は、私はthis のような記事を参照している私はImageviewでクラスGIFViewを設定したいですが、適切な例を得られない。あなたはは、どのように私はImageViewの

おかげ
NIK

答えて

3

をビューを設定することができることを私に適切な例と説明を与えることができ、私はそれがに(ImageViewの「ビューの設定」に意味があるとは思いませんあなたが提案している方法で)、私はあなたがこの場合にはImageViewを必要とするとは思わない。 ImageViewクラスに依存せずにGIFを描画できるカスタムViewを実装しました。使用するだけです!

最小限の変更で、GIFViewが動作すると仮定すると、GIFViewをインスタンス化してから、メインレイアウトに追加することができます。次のようにあなたのonCreateになりましょう:あなたはレイアウトからImageViewのを取り除くことができます

@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); 

} 

完全に、ちょうどコードでインスタンス化されたあなたのGIFViewを使用しています。あるいは、GIFViewをスタンドアロン(非内部)クラスにして、完全なパッケージ名を指定することでXMLレイアウトに追加することができます。

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

それはXMLを使用する際に期待されるものであるため、また、ビューのコンストラクタGIFView(Context context, AttributeSet attrs)を記述する必要があります。

関連する問題