3

整个代码是这样的

首先是自定义视图类

public class AnimatedView extends View {
private static final String TAG="ANIMATEDVIEW";
    private Movie mMovie;
     private long mMoviestart;
    private InputStream is;
    private Context mContext;
public AnimatedView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext=context;
    is=context.getResources().openRawResource(R.drawable.ic_action_search);
    mMovie=Movie.decodeStream(is);
    //this.draw(context.getSystemService(Context.))
    this.setWillNotDraw(false);
    Log.v(TAG,"calling constructor of 2 params");
    // TODO Auto-generated constructor stub
}
    public void setImageId(int id){
is=mContext.getResources().openRawResource(id);
mMovie=Movie.decodeStream(is);
this.invalidate();
this.setWillNotDraw(false);
this.setWillNotCacheDrawing(false);
    }
public AnimatedView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    is=context.getResources().openRawResource(R.drawable.ic_action_search);
    mMovie=Movie.decodeStream(is);
    Log.v(TAG,"calling constructor of 3 params");
    // TODO Auto-generated constructor stub
}

public AnimatedView(Context context) {
    super(context);
    is=context.getResources().openRawResource(R.drawable.ic_action_search);
    mMovie=Movie.decodeStream(is);
    Log.v(TAG,"calling constructor of 1 params");
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);       
    final long now = SystemClock.uptimeMillis();
    if (mMoviestart == 0) {
        mMoviestart = now;
    }
    int startTime = (int) (now - mMoviestart);
    int dur = 1;
    if (mMovie != null)
        dur = mMovie.duration();
    final int relTime = startTime % dur;
    mMovie.setTime(relTime);
    mMovie.draw(canvas, 10, 10);
    this.invalidate();
}
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.v(TAG,"On onMeasure==>");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(
                parentWidth * 2, parentHeight);
    }

     }

适配器代码:

public class AnimAdapter extends BaseAdapter {
private Context mContext;
private List<Item> piclist;
private int rowResID;

public AnimAdapter(Context mContext, List<Item> piclist, int rowResID) {
super();
this.mContext = mContext;
this.piclist = piclist;
this.rowResID = rowResID;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return piclist.size();
}

@Override
public Object getItem(int pos) {
    // TODO Auto-generated method stub
    return piclist.get(pos);
}

@Override
public long getItemId(int pos) {
    // TODO Auto-generated method stub
    return pos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     Item imgAnimItm= piclist.get(position);
        LayoutInflater inflate=(LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=inflate.inflate(rowResID, parent, false);
        TextView animTxtName= (TextView)v.findViewById( R.id.helloTxt );
        if( animTxtName != null )
            animTxtName.setText( imgAnimItm.getName());
        AnimatedView animView= (AnimatedView)v.findViewById( R.id.anim_view );
        if( animView!= null )
            animView.setImageId(imgAnimItm.getId());
        return v;
}

 }

列出活动代码:

public class CustomActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom);
    ArrayList<Item> alarmList = new ArrayList<Item>();
    Item a = new Item("Low memory", R.drawable.angel_pray_anim);
    alarmList.add(a);
    a = new Item("Process #345 stopped", R.drawable.anim_angry);
    alarmList.add(a);
    a = new Item("Low memory", R.drawable.engg_anim);
    alarmList.add(a);
    a = new Item("Process #456 stopped", R.drawable.finger_anim);
    alarmList.add(a);
    AnimAdapter AnimAdapter = new AnimAdapter(this, alarmList,
            R.layout.anim_row_layout);
    setListAdapter(AnimAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_custom, menu);
    return true;
}
}

anim_row.xml 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="horizontal" >

<TextView
    android:id="@+id/helloTxt"
    android:layout_width="200dp"
    android:layout_height="wrap_content" >
</TextView>

<com.example.customviewdemo.AnimatedView
    android:id="@+id/anim_view"
    android:layout_width="50dip"
    android:layout_height="50dip" >
</com.example.customviewdemo.AnimatedView>

</LinearLayout>

主要活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="No items" >
</TextView>

</LinearLayout>

物品类别:

public class Item {
private String name;
private int id;

public Item(String name, int id) {
    super();
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

}

这是我尝试使用自定义视图并在列表视图中显示动画 gif 文件的整个代码,但 AnimatedView 的 onDraw() 方法没有被调用,因此它没有显示任何内容。

为什么 onDraw() 没有被调用?

我还尝试直接在适配器的布局中添加 AnimatedView 的实例,但结果相同。

4

0 回答 0