我有
imageView
TextView
Rectangle drawn from canvas
我在做
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
setContentView(R.layout.description);// If this get invoked then how will `drawView` will get overlapped.
// then normal
}
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setColor(Color.BLACK);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint );
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint );
}
修改1
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_new_oct23);
episode_image=(ImageView)findViewById(R.id.imageView_EpisodeImage);
characters=(ImageView)findViewById(R.id.button_Characters);
progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay);
DrawView drawView = new DrawView(this);
progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay);
DrawView drawView = new DrawView(this);
// drawView = new DrawView(this);
progressBarLayout.addView(drawView);
}
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
/* paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setColor(Color.BLACK);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint );
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint );
*/
draw(canvas);
}
@SuppressLint("NewApi")
public void draw(Canvas canvas){
int width = 0;
int height = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
width = size.x;
height=size.y;
}else{
Display d = w.getDefaultDisplay();
width = d.getWidth();
height=d.getHeight();
}
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
width=width-20;
int margin=width/SIZE;
int begin=10;
paint.setStrokeWidth(3);
for (int iTmp=0;iTmp<SIZE;iTmp++){
begin=begin+(iTmp)*(margin);
// begin=begin-2;
System.out.println("onkar "+begin);
paint.setColor(Color.CYAN);
canvas.drawRect(begin, 20, margin-2, 30, paint);
}
}
}
// drawView = new DrawView(this);
progressBarLayout.addView(drawView);`
答案 0 :(得分:1)
你应该有这样的方法:
// set content view
setContentView(R.layout.description);
// get the parent of imageview and textview, i'll suppose its LinearLayout.
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
// then add your view, since its a linear layout it will automatically be added below textview. for other layouts you use LayoutParams to position your View.
ll.addView(new DrawView());
答案 1 :(得分:0)
您可以使用FrameLayout堆叠视图,并使用SurfaceView作为底视图进行绘制。