main.xml hiding the view help
I am unable to display both the setContentView(R.layout.main) and View together. I think I am not getting the concept right. Could anyone explain me where I am going wrong. Thank you. //please read the comment in code
I am trying to display a image on main.xml using BitmapFactory.
public class TryGraph extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//I think this is where I need your help
setContentView(new myView(this));//I want this to be displayed in main.xml
}
private 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
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sinewave);
Bitmap resizedBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
300, 143);
canvas.drawBitmap(resizedBitmap, 60, 50, null);
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(5);
canvas.drawRect(250,255,260,250, myPaint);
}
}
}
THE XML FILE IS
When you call setContentView
you are telling the device to load the entire layout that should be displayed to the user. In most cases this view will occupy the entire screen. Now this Layout file is considered the root and may contain child views, one of which should be your ImageView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/banner"
android:text="hello world"
>
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sampleimage"
/>
<?LinearLayout>
This ImageView can now be accessed by code through the use of findViewById(R.id.myImageView)
and you can then use the BitmapFactory
to set your image. If the image is not going to be changed, you can just set it in the layout file android:src="@drawable/sampleimage"
精彩评论