位图没有完全显示,并且没有内存不足异常

时间:2013-12-24 09:04:44

标签: android bitmap

我想使用自定义视图在屏幕上显示时钟及其指针。 我正是这样做的,因为我想稍后在代码中更改时钟指针的方向。

代码 -

public class Clockwork extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        MyViews mv=new MyViews(this);
        LinearLayout l1=new LinearLayout(this);
        LinearLayout l2=new LinearLayout(this);
        LinearLayout l3=new LinearLayout(this);
        ScrollView sc=new ScrollView(this);
        Button b1=new Button(this);
        Button b2=new Button(this);
        TimePicker tp=new TimePicker(this);

        b1.setText("Okay");
        b2.setText("Cancle");
        l3.setOrientation(LinearLayout.VERTICAL);
        l3.addView(b1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        l3.addView(b2, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        l2.setOrientation(LinearLayout.HORIZONTAL);
        l2.addView(tp);
        l2.addView(l3,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        l1.addView(mv,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        l1.addView(l2);
        sc.addView(l1,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        this.setContentView(sc);

    }
class MyViews extends ImageView{

    Bitmap clock,mh,hh;
    public MyViews(Context context) {
        super(context);
        clock=BitmapFactory.decodeResource(getResources(), R.drawable.clock, null);
        hh=BitmapFactory.decodeResource(getResources(), R.drawable.hhand, null);
        mh=BitmapFactory.decodeResource(getResources(), R.drawable.mhand, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(clock, 0, 0, null);
        canvas.drawBitmap(mh, 0, 0, null);
        canvas.drawBitmap(hh, 0, 0, null);
        super.onDraw(canvas);

    }

}


}

输出 - enter image description here

这就是logcat所说的 -

12-24 14:11:06.178: I/dalvikvm(281): Could not find method android.widget.Button.setBackground, referenced from method sarath.apps.SiLeNcE.MainActivity.mtv
12-24 14:11:06.178: W/dalvikvm(281): VFY: unable to resolve virtual method 3984: Landroid/widget/Button;.setBackground (Landroid/graphics/drawable/Drawable;)V
12-24 14:11:06.238: D/dalvikvm(281): VFY: replacing opcode 0x6e at 0x0018
12-24 14:11:06.248: D/dalvikvm(281): VFY: dead code 0x001b-0049 in Lsarath/apps/SiLeNcE/MainActivity;.mtv (Landroid/view/View;)V
12-24 14:11:17.660: D/dalvikvm(281): GC_FOR_MALLOC freed 5424 objects / 315672 bytes in 55ms
12-24 14:11:53.428: W/KeyCharacterMap(281): No keyboard for id 0
12-24 14:11:53.428: W/KeyCharacterMap(281): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-24 14:16:47.358: W/IInputConnectionWrapper(281): showStatusIcon on inactive InputConnection
12-24 14:16:53.058: D/dalvikvm(281): GC_EXPLICIT freed 5122 objects / 263048 bytes in 183ms

我做错了什么?

1 个答案:

答案 0 :(得分:1)

看起来你的画布比你的视图更大。您需要使用setLayoutParams更改视图大小。