问题描述
我试图让我的getView方法在我的customView类的canvasAdapter.java类中工作。我试图重新实现从https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/ui/layout/gridview.html获得的imageView示例。这是我的canvasAdapter.java类。
public class CanvasAdapter extends BaseAdapter {
public Context getmContext() {
return mContext;
}
private Context mContext;
private AttributeSet attributeSet = new AttributeSet() {...};
private Random randInt = new Random();
public CanvasAdapter(Context c)
{
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
MyCanvas dummyCanvas = new MyCanvas(mContext,attributeSet);
if(convertView == null){
dummyCanvas.clearCanvas();
}
for (Integer[] row: mThumbIds) {
for (Integer ele: row) {
ele = position;
}
}
return dummyCanvas;
}
// Keep all canvas objects in array
public Integer[][] mThumbIds = { // random values
{ randInt.nextInt(3),randInt.nextInt(3) },{ randInt.nextInt(3),randInt.nextInt(3) }
};
}
我试图让getView与MyCanvas一起使用:
public class MyCanvas extends View {
private Paint pixelPaint;
public Bitmap getMyBitmap() {
return myBitmap;
}
private Paint myBitMapPaint;
public Bitmap myBitmap;
private Canvas myCanvas;
CanvasAdapter canvasAdapter = new CanvasAdapter( getContext() );
private Random randInt = new Random();
public MyCanvas(Context context,AttributeSet attrs)
{
super(context,attrs);
myBitMapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
pixelPaint = new Paint();
pixelPaint.setAntiAlias(true);
pixelPaint.setStyle(Paint.Style.stroke);
pixelPaint.setColor(pixelPaintColor);
pixelPaint.setstrokeCap(Paint.Cap.ROUND);
pixelPaint.setstrokeWidth(42);
}
...
Integer[][] shapePosition = canvasAdapter.mThumbIds;
int shapeheight = 1;
int shapewidth = 1;
public void drawShapes() // fill in grid
{
for(int row = 0; row < shapePosition.length; row++){
for(int col = 0; col < shapePosition[row].length; coL++){
switch(shapePosition[row][col])
{
case 0: pixelPaint.setColor(Color.RED);
pixelPaint.setstrokeWidth(60);
myCanvas.drawCircle(row*shapeheight + 0.5f,col*shapewidth + 0,0.5f,pixelPaint);
case 1: pixelPaint.setColor(Color.GREEN);
myCanvas.drawRect(row*shapeheight + 0.1f,col*shapewidth + 0.1f,row*shapeheight + 0.9f,col*shapewidth + 0.9f,pixelPaint);
case 2: pixelPaint.setColor(Color.BLUE);
myCanvas.drawRect(row*shapeheight + 0.1f,pixelPaint);
}
}
}
}
public void clearCanvas() {
myBitmap = Bitmap.createBitmap(myBitmap.getWidth(),myBitmap.getHeight(),Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(myBitmap);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int w = myBitmap.getWidth();
int h = myBitmap.getHeight();
drawShapes();
canvas.drawBitmap(myBitmap,myBitMapPaint);
}
}
我认为是什么使getView内部的MyCanvas逻辑感到困惑。我知道我希望画布绘制的对象位于“视图”中设置的位置。我只是不知道该怎么做。 另外,我在主要活动课上遇到错误。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GridView gridview = (GridView) findViewById(R.id.gridview);
//gridview.setAdapter(new CanvasAdapter(this));
GridView gridView = (GridView)findViewById(R.id.gridview);
CanvasAdapter canvasAdapteradapter = new CanvasAdapter(this);
gridView.setAdapter(canvasAdapteradapter);
}
}
错误: 致命异常:主要 流程:com.example.midterm,PID:17880 java.lang.classCastException:com.example.midterm.CanvasAdapter $ 1无法转换为android.content.res.XmlBlock $ Parser
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)