I want to keep the same size of the bitmap for the canvas, because when I add the custom view to the LinearLayout shows the canvas with different size and I want to set the size of the canvas like bitmap object.
Part of the code:
public class TESTActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout l = new LinearLayout(this);
l.setOrientation(1);
Button b1 = new Button(this);
Button b2 = new Button(this);
View mV = new MyView(this);
l.addView(b1);
l.addView(b2);
l.addView(mV);
setContentView(l);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
mBitmap = Bitmap.createBitmap(480, 300, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
...
}
...
}
}
The Canvas class holds the "draw" calls. Canvas(Bitmap bitmap) Construct a canvas with the specified bitmap to draw into. canvas will take the size of the the draw object. so by setting size of bitmap you can set size of canvas.
If you want to draw on canvas with your custom height and width you have to call setContentView(android.view.View yourView , android.view.Viewgroup.LayoutParam yourLayout) in your activity class.Because by default setContentView(View view) method use full width and height.So you have to use its overloaded method with two parameter along with your desired. See documentation for more info.And don`t use only LayoutParams() constructor to create its object. Use it by writing its full path like android.view.ViewGroup.LayoutParams. Because there are some other classes with same name in Android SDK.If you only use LayoutParams Eclipse may not find correct class so use full path.
MyView customView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new MyView(getApplicationContext());
android.view.ViewGroup.LayoutParams lp = new android.view.ViewGroup.LayoutParams(100,200);//100 is width and 200 is height
setContentView(customView, lp);
customView.setOnClickListener(this);
}`
Related
I wrote the following code and it works well. But I have other purpose. I want to click only on a view to doing the operations.First, Please see the following image:
MY CODE IS AS FOLLOWS:
public class MainActivity extends Activity {
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle bundle)
{ super.onCreate(bundle);
relativeLayout = new RelativeLayout(getApplicationContext());
setContentView(relativeLayout);
A a = new A(this);
relativeLayout.addView(a);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
B b = new B(getApplicationContext());
relativeLayout.addView(b);
}
});
}
}
class A extends View {
Paint paint;
A(Context context) {
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawRect(20,60,100,150,paint);
}
}
class B extends View {
Paint paint;
B(Context context){
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas){
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
canvas.drawRect(100,150,200,250,paint);
}
}
when I run the above code I can see the green rectangle after press on the red rectangle. But the problem is that when I press another places on the screen I can do this operations also. I want that only I can see the green rectangle to press on the red rectangle and not in the another places on the screen to doing this operations.
Use onTouch event
a.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getX(0)>=20 && event.getY(0)>=60 && event.getX(0)<=160 && event.getY(0)<=150) {
B b = new B(getApplicationContext());
relativeLayout.addView(b);
}
return true;
}
});
You are defining the red square's parameters but not the parameters of the canvas in which you are drawing. You are creating the view (A) without defining the width and height of it, so it is set to match_parent by default, which means it will take the whole size of your RelativeLayout (the whole screen). So, when you click "outside" the red square you are actually clicking the view (A).
Try to define an specific height and width for the view in which you are drawing, like this.
A a = new A(this);
a.setLayoutParams(new RelativeLayout.LayoutParams(300,300));
Remember that LayoutParams takes pixels as parameters, so you should really convert the dps to px as specified here
Also, setting some background colors to your views (relativeLayout, A) will help you visualize what you are doing.
# nukeforum, your guess helped me very much. I thank all of you. My problem was exactly from the canvas and its size. I added the following operation in my code and solved my problem.
relativeLayout.addView(a,70,70);
For A class, I changed as follows:
canvas.drawRect(10,20,30,40,paint);
So basicaly the theory is this: i want to create a class that holds a bitmap, which is loaded from a resource. then i want to create a new object of that class and add it to a layout.
So far i've managed to do everything except idk, how to add the bitmap to a layout.
And i want it to be displayed on an existing layout.
bitmap class:
class BitmapView extends View
{
public BitmapView(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.wave);
// canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
and i call it with setContentView(new BitmapView(this));
but this draws the picture over the whole screen. i want it to display the background that was set in a previous layout. Also is there any way i can set bitmap size?
Thanks
Use an Imageview for displaying bitmap
ImageView imgView=new ImageView(context);
imgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imgView.setBitmapImage(bitmap); //Your bitmap goes here.
setContentView(imgView);
use setImageBitmap(bmp). Put an ImageView inside your layout. Reference it with ImageView im = (ImageView) findViewById(R.id.myBitmapId);. When you create your bitmap simply do im.setImageBitmap(bmp);
I can add this logo to the canavas if I do this:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200,200);
ImageView boo = new ImageView(ExampletouchActivity.this);
boo.setImageResource(R.drawable.ic_launcher);
boo.setLayoutParams(lp);
fr.addView(boo); //this works fine
But if I try to add the same logo to the canvas like this it does not show anything up:
fr.addView(new Toucher(ExampletouchActivity.this));
My Toucher class is like so...
public class Toucher extends View{
ImageView boo;
public Toucher(Context context)
{
super(context);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200,200);
boo = new ImageView(context);
boo.setImageResource(R.drawable.ic_launcher);
boo.setLayoutParams(lp);
}
#Override
public void onDraw(Canvas c)
{
boo.draw(c);
}
I have been trying for ages and cannot work out why this imageview will not draw itself to the canvas?
Many Thanks
Layout and Canvas aren't the same thing. Layout is "draw this for me", and Canvas is "I'm drawing this". In your working example, you're taking a static image and putting it into a Layout.
When you are using the canvas, in a view things are different, so try using:
canvas.drawBitmap(bg, src, dst, paint);
I'm trying to call onDraw method on Button click ,button the view is updated only once also onDraw method is called . But no change in the position of line.
I have this custom view
public LineSeekbar(Context context) {
super(context);
this.setWillNotDraw(false);
setNewX(130);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("GRAPH","draw");
paint = new Paint();
paint.setARGB(225, 215, 10, 20);
paint.setStrokeWidth(2);
paint.setStyle(Style.FILL);
canvas.drawLine(130,900,getNewX(),100, paint);
setNewX(getNewX()+15);
}
Calling this from activity class
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView = new LineSeekbar(LineActivity.this);
LineView.setLayoutParams(new LayoutParams(500,500));
LineView.onMeasure(500,500);
LineView.invalidate();
LineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LineView.invalidate();
LineView.draw(cv);
}
});
You should call an instance with small letter because it is not a class: lineView insted of LineView.
Since you are calling it from another thread (UI) you should say
LineView.postInvalidate();
When you create LineView instance for the first time, onDraw will be executed on its own and show the first line.
It is not clear what is LineView.draw(cv); You can draw the background image in onDraw just before drawing the line. You can use onSizeChange method in the custom view to find its real dimensions and resize your bitmap...
In onDraw insert a line
Log.e("LineView", Integer.toString(getNewX()));
and then in DDMS - LogCat watch the output when you press the button.
Try this:
You didn't made an object of the LineView class like you should. You should use small letters for instances.
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView lineView = new LineSeekbar(LineActivity.this);
lineView.setLayoutParams(new LayoutParams(500,500));
lineView.onMeasure(500,500);
lineView.invalidate();
lineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lineView.invalidate();
lineView.draw(cv);
}
});
Solved it ,I placed custom view in xml layout ,its working fine.
My project is about image processing in android.I have a bitmap that I have loaded from a resource file (an PNG image). I want to draw it. But I couldn't.
Here my code snippet:
mB = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
Canvas c = new Canvas(mB);
Paint p = new Paint();
c.drawBitmap(mB,0,0,p);
it didn't work. Is the code true? .Is there any thing more that I must do?
You should use an ImageView instead and load it by
imageView.setImageResource(R.drawable.picture);
If you want to manually draw it with a Canvas, you have to use a canvas that is passed into a draw() method and implement a Custom View.
Update to add example CustomView:
public class CustomView extends View {
private Paint mPaint;
private Drawable mDrawable;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mDrawable = context.getResources().getDrawable(R.drawable.some_drawable);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mDrawable.draw(canvas);
}
}
There are a couple of things you are missing.
First, I think you're misunderstanding the Canvas(Bitmap b) constructor. The Bitmap passed in there is one that the Canvas will draw into. This could be just a new Bitmap that you constructed.
Second, it is recommended that you use the Canvas that is passed to you in your View's onDraw method. Presumably that View is one from your Activity, either fetched from your XML layout via findViewById or constructed and passed to setContentView in the Activity's onCreate() method.
So, you are going to have to subclass View and override the onDraw method to get your drawing done. Something like:
public class MyView extends View {
#Override
public void onDraw (Canvas c) {
Bitmap mB = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.picture);
c.drawBitmap(mB, 0, 0, null);
}
}
Then, in your Activity, you'll need to create an instance of your new View and pass it to the Activity via setContentView:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MyView(this);
setContentView(mv);
}
You can instead call the setContentView(View v, ViewGroup.LayoutParameters lp) overload if you want to set up the LayoutParameters.
I haven't tested any of this, but it should at least get you on the right path.