Android - 为什么我不能直接看到VIEW?

时间:2011-09-17 06:56:48

标签: android

我已经构建了一个名为myview的新视图,其中包括OnDraw部分。

我尝试在setContentView(myview)中使用Oncreate,然而,它失败并显示出一些错误。

然后我使用addview函数将此视图添加到我的原始布局,并setContent(R.layout.main)

最后我可以在屏幕上看到结果。

问题是什么?为什么我不能直接使用setContentView(myview)来显示结果?

 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);   
       myview2=new MyView2(this);
       setContentView(R.layout.main2);
       RelativeLayout layout= ( RelativeLayout )findViewById(R.id.dashpage);        
       layout.addView(myview2);

    }

class MyView2 extends View{

        public MyView2(Context context) { 
            super(context);
        }
        Bitmap themometer = BitmapFactory.decodeResource(getResources(),  
                   R.drawable.the);
        Matrix matrix2 = new Matrix();
        Bitmap themometerbmp = Bitmap.createBitmap(themometer, 0, 0, themometer.getWidth(),  
                themometer.getHeight(), matrix2, true);

        public void onDraw(Canvas canvas2)
        {
            canvas2.drawBitmap(themometerbmp,90,5,null);
        }
    }

1 个答案:

答案 0 :(得分:0)

我不认为0 1 2 3 4 5 6 7 8 9 是问题所在。您正在尝试使用可绘制对象。试试这个:

在MyView2中添加功能

setContentView

然后,代替

public Bitmap getBitmapFromDrawable (Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 
        drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

使用

Bitmap themometer = BitmapFactory.decodeResource(getResources(), R.drawable.the);