当使用new关键字实例化customview的对象时,不会调用Android draw()

当使用new关键字实例化customview的对象时,不会调用Android draw(),android,android-arrayadapter,android-custom-view,ondraw,Android,Android Arrayadapter,Android Custom View,Ondraw,我有一个自定义视图,其中有一个对应的自定义布局xml,我想在列表视图中显示它(因此我可以在列表中显示多个) 作为测试,我在onCreate()中执行了此操作: 与: 尽管这是将cstmView放入列表,但它并没有在cstmView中运行重写的draw功能 我认为问题在于new cstmView(this.getApplicationContext()),因为无论是否使用列表视图,都会出现相同的问题。new关键字是否未运行draw() 这是应该在cstmView.java中调用draw的方法 pu

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

作为测试,我在onCreate()中执行了此操作:

与:

尽管这是将cstmView放入列表,但它并没有在
cstmView
中运行重写的
draw
功能

我认为问题在于
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;
    }

call方法
invalidate()
它将隐式地调用onDraw()我应该在哪里调用它?在init()中或在onCreate()中的new关键字之后?在cstView的构造函数中,尽管调用draw(),但它不会显示在draw中实现的圆角和其他渲染。是因为我在arrayAdapter中调用了
rowView=inflater.inflate(资源,父项,false)?我更新了问题并发布了方法代码here@PrachiSingh只需使用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);

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