【发布时间】:2019-07-31 11:29:35
【问题描述】:
我有一个带有相应自定义布局 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 放入列表中,但它并没有运行 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;
}
【问题讨论】:
-
这里也贴出绘制方法代码
-
@PrachiSingh 只需使用绘图功能更新代码。
-
我无法理解你的目的。如果你分享你想做的事,我可以提供帮助。
标签: android android-arrayadapter android-custom-view ondraw