java 设置画布大小_java – 如何设置画布大小?

我试图实现一个简单的应用程序,在主要活动中绘制一个黑色矩形,按下按钮.例如,在MainActivity中:

private Button button1;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button1=(Button)findViewById(R.id.button);

button1.setOnClickListener(new OnClickListener(){

public void onClick(View v) {

switch(v.getId()){

case R.id.button:

LinearLayout ll=(LinearLayout)findViewById(R.id.linearLayout1);

System.out.println(ll.getWidth()+" "+ll.getHeight());

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ll.getWidth(),ll.getHeight());

YourView yourView = new YourView(getBaseContext());

yourView.setBackgroundColor(Color.WHITE);

ll.addView(yourView,params);

break;

}

}

});

}

在YourView类中:

private Bitmap savedBitmap;

public YourView(Context context) {

super(context);

}

public void onDraw(Canvas canvas) {

super.onDraw(canvas);

System.out.println(canvas.getWidth()+" "+canvas.getHeight());

Paint textPaint = new Paint();

textPaint.setARGB(255,0);

textPaint.setTextAlign(Paint.Align.RIGHT);

textPaint.setTextSize(11);

textPaint.setTypeface(Typeface.DEFAULT);

canvas.drawColor(Color.WHITE);

System.out.println(canvas.getWidth());

System.out.println(canvas.getHeight());

canvas.drawRect(200,20,500,100,textPaint);

}

main.xml:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Push the button and draw a Rect" />

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Button" />

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent" >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值