Trying to add Imageview to canvas via FrameLayout.addView(); - android

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);

Related

How to understand where you put your widgets in android layouts programmatically?

I'm currently making a simple game with the surfaceview but I find it hard to position the buttons or texts within the Relative layout programmatically in android. For example, in the sample below Im setting the relativelayouts with texts in a relative layout, and I want to set it to the right side of the screen with few margins to the right. How should I understand where I'm positioning the contents ? And are there some tips on positioning contents programmatically?
RelativeLayout Rs = new RelativeLayout(this);
Rs.setBackgroundResource(R.drawable.btn);
Rs.setGravity(Gravity.CENTER);
Regame.addView(RR,400,150);
Rs.setX(0);
txt= new TextView(this);
I guess it would be sufficient if you accessed the canvas from within the surfaceView.Probably would be more efficient if you used onSizeChange
for the width and height but i guess the canvas getWidth and getHeight would suffice.Also my example is just drawing once the surface is created, you'll have to make your own update logic.Also i am ditching the relative layout but i think this could suffice your needs:
public class GameView extends SurfaceView {
protected SurfaceHolder holder;
public GameView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
});
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
int width=canvas.getWidth();
int height=canvas.getHeight();
//Just for example
int desiredHeight=height/2;
int desiredMargin=10;
String desiredText="Some text";
int textWidth=Math.round(paint.measureText(desiredText));
int desiredWidth=width-textWidth-desiredMargin;
canvas.drawText(desiredText, desiredWidth,desiredHeight, paint);
}
}

Can you put/restrict a canvas to a layout

I'm quite new to Android programming and I'm stuck on how to get this right.
My situation is I want to draw a rectangle inside a RelatativeLayout and be able to change the shape of the rectangle dynamically relative to the size of the RelativeLayout which does not encompass the whole screen.
My problem is when I try to implement this, the size of the rectangle is still relative to the screen size
Is there a way where I can restrict the canvas size to the RelativeLayout size or is there a better way?
Here is part of my code from the maim activity
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
blueView = new Bar(this);
blueView.setLayoutParams(rl);
blueLayout.addView(blueView);
And I also have a Bar class that extends View
public class Bar extends View{
Rect theBar;
public Bar(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
theBar = new Rect();
theBar.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
Paint blue = new Paint();
blue.setColor(Color.BLACK);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(theBar, blue);
}
}

Android - add image from a class to a layout

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);

How can I draw text to a view in a circular pattern in Android

I want to add text in a circle or half circle programmatically, in such a way that instead of having a circle with line edges, the edges are the words. See image for a better explanation.
How can I do this in Android, or what resources could I read in order to help me with this problem?
In order to do this, you will need to draw your text onto a Canvas. Any subclass of View is passed a Canvas in onDraw() that you can use to draw your custom text. The method drawTextOnPath() lets you put text on any Path object you choose. You can create a semi-circle path by creating a new instance and using addArc().
you can Use Below Code. and Make it as you Want your Textview.
Here if you want Something as Backgroung image then use setBackgroundResource(R.drawable.YOUR_IMAGE);
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsView(this));}
static public class GraphicsView extends View {
private static final String QUOTE = "text in a half-circle";
private Path circle;
private Paint cPaint;
private Paint tPaint;
public GraphicsView(Context context) {
super(context);
int color = Color.argb(127, 255, 0, 255);
circle = new Path();
circle.addCircle(230, 350, 150, Direction.CW);
cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
cPaint.setStyle(Paint.Style.STROKE);
cPaint.setColor(Color.LTGRAY);
cPaint.setStrokeWidth(3);
// For Background Image
setBackgroundResource(R.drawable.YOUR_IMAGE);
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//TextColor you want to set
tPaint.setColor(Color.BLACK);
//TextSize you want to set
tPaint.setTextSize(50);}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);}
}
}
try it out. hope it will help you.

set the canvas size - Android

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);
}`

Categories

Resources