首页 > 解决方案 > 使用 new 关键字实例化 customview 对象时,不会调用 Android draw()

问题描述

我有一个带有相应自定义布局 xml 的自定义视图,我想在列表视图中显示它(因此我可以在列表中包含多个)。

作为一个测试,我在 onCreate() 中做了这个:

cstmView s = findViewById(R.id.card);
s.setImg(R.drawable.ac);

和:

<views.cstmView
        android:layout_width="395dp"
        android:layout_height="72dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

所以我知道视图正在被初始化和绘制cstmView.java

但是,我想创建一个列表视图,以便cstmView在可滚动列表中有多个。这就是我所做的(作为测试,我只是在列表视图中放了一个 cstmView):

Listv = new ArrayList<>();
//adding just one cstmview
Listv.add(new cstmView(this.getApplicationContext()));
//this is my custom adapter to accept cstm layour rathern than a textview
cstmAdapter adapter = new cstmAdapter(this, R.layout.cstm_view, ListV);
ListView cstmListView = findViewById(R.id.cstmListView);
cstmListView(adapter);

尽管这会将 cstmView 放入列表中,但它并没有drawcstmView.

我认为问题在于,new cstmView(this.getApplicationContext())因为无论有没有列表视图都会出现同样的问题。new关键字不运行吗draw()

这是应该在 cstmView.java 中调用 draw 的方法

public void init(Context context){
    System.out.println("INIT");
    // To instantiate an XML layout file from the already configured R.layout
    LayoutInflater.from(context).inflate(R.layout.cstm_view, this);

    // Get elements in layout

    text = (TextView)findViewById(R.id.title);
    back = (ImageView)findViewById(R.id.img);

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();

    // to convert
    cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    // to draw the above
    setWillNotDraw(false);

}

然后 on draw 函数创建一些位图等等......

 @Override
public void draw(Canvas canvas) {
    System.out.println("DRAW");
    // create a bitmap with this context height and width
    Bitmap offscreenBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas offscreenCanvas = new Canvas(offscreenBitmap);

    super.draw(offscreenCanvas);

    if (maskBitmap == null) {
        maskBitmap = createMask(getWidth(), getHeight());
    }

    offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
    canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);


}

private Bitmap createMask(int width, int height) {

    System.out.println("CREATE MASK");
    Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
    Canvas canvas = new Canvas(mask);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);

    canvas.drawRect(0, 0, width, height, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

    return mask;
}

更新

调用invalidate()构造函数解决了这个问题。但是,我仍然没有看到draw应该在屏幕上进行的渲染。有什么问题arrayAdatper吗?:

@Override
    public View getView(int position, View convertView, ViewGroup parent){
        View rowView = convertView;
        //if data already exists in list reuse the data rather than re inflate from scratch
        if(rowView == null) {
            LayoutInflater inflater;
            inflater = LayoutInflater.from(context);

            rowView = inflater.inflate(resource, parent, false);
        }
        return rowView;
    }

标签: androidandroid-arrayadapterandroid-custom-viewondraw

解决方案


调用方法invalidate(),它将隐式调用onDraw()


推荐阅读