我正在尝试将包含圆圈的画布绘制到先前通过findViewById()定义的视图中
View v = findViewById(R.id.view1);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawCircle(v.getMeasuredWidth() / 2, v.getMeasuredHeight() / 2, v.getMeasuredWidth() / 4, paint);
v.draw(c);
但视图中没有绘制圆圈
我的整个活动:
package com.burrlieexample.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class InstructionsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("DEBUG", "InstruciotnActivity");
setContentView(R.layout.activity_instructions);
Button okay = (Button) findViewById(R.id.okButton);
okay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
Log.d("DEBUG", "onFocusChanged");
//im doing this stuff in onWindowfocusChanged because getWidth returns 0 in onCreate
View v = findViewById(R.id.view1);
Log.d("DEBUG", "" + v.getMeasuredWidth() + ", " + v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawCircle(v.getMeasuredWidth() / 2, v.getMeasuredHeight() / 2, v.getMeasuredWidth() / 4, paint);
v.draw(c);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
并且View在我的布局xml文件中就像这样
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D0F3FF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.burrlieexample.test.InstructionsActivity" >
<Button
android:id="@+id/okButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:background="#00455F"
android:hapticFeedbackEnabled="true"
android:text="Ok!" />
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/okButton"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
因此,当Activity开始时,我看到的唯一内容是按钮和我的相对布局的背景
答案 0 :(得分:3)
这样做很简单: 绘制圆圈以查看 当UI不需要在应用程序中进行动态更改时,绘制到视图是更好的选择。 这可以通过扩展View类并定义onDraw()回调方法来实现。在View组件的onDraw()中,使用各种Canvas.draw ...()方法(例如:canvas.drawCircle(x / 2,y / 2,radius,paint)),使用为您的所有绘图提供的Canvas; )。 onDraw()是最初绘制视图时调用的回调方法。
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
}
答案 1 :(得分:-2)
您将位图提供给垃圾收集器。你从来没有在视图上画任何东西。因此,视图中没有任何内容。