我正在尝试在画布上绘制 GridView。我有一个自定义视图,我正在覆盖它的 onDraw 来实现这一点。
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = (GridView)li.inflate(R.layout.activity_main, null);
gridView.setAdapter(mImageAdapter);
gridView.setDrawingCacheEnabled(true);
Bitmap cac = getDrawingCache();
if(cac != null) {
canvas.drawBitmap(cac, 0, 0, new Paint());
} else {
gridView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
gridView.layout(0, 0, gridView.getMeasuredWidth(), gridView.getMeasuredHeight());
gridView.draw(canvas);
}
canvas.restore();
}
这是 GridView 的 XML:
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center">
GridView 被绘制,但所有图像都绘制在一行中。我想我可能必须覆盖这个 GridView 上的 onMeasure,但我不确定如何继续。请帮帮我!