Put a Image over an activity - android

How I can put an image over an activity (something like the image below) but with the buttons and other widgets under this image receiving the touch events?
image http://images.macworld.com/appguide/images/android/558/2908791394402692/5582908791394402692_1.jpg

You can create a transparent Activity like this How do I create a transparent Activity on Android? and make a Layout with a fullscreen ImageView

I solve the problem override the draw function.
public class TutorialLinearLayout extends LinearLayout {
public TutorialLinearLayout(Context context) {
super(context);
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
Bitmap tutorial = BitmapFactory.decodeResource(getResources(), R.drawable.tutorial);
canvas.drawBitmap(tutorial, 0, 0, null);
tutorial.recycle();
}
}

Related

Android. Button does not work in Canvas

Button does not work in Canvas. What you need to change / add the caller to a message? The button is created but does not respond when you press. Is it possible that the canvas is over the button?
public class ButtonInCanvas extends AppCompatActivity implements View.OnClickListener {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = new Button(ButtonInCanvas.this);
button.setOnClickListener(this);
button.setText("OK!");
setContentView(new BtInCanvas(ButtonInCanvas.this));
}
public class BtInCanvas extends View {
public BtInCanvas(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
button.layout(50,50,300,300);
button.draw(canvas);
}
}
#Override
public void onClick(View v) {
Toast.makeText(this,"OK!",Toast.LENGTH_LONG).show();
}
}
Let refer to the link Android drawing button to canvas with custom view?
"You cannot insert a button into canvas. Canvas is an interface for bitmap or a bitmap buffer for a view. You can only draw other bitmap or pixels in it, not insert an object or a widget.
There are some solutions:
as Nikolay suggested, use a FrameLayout and create two layers (views), first your custom view and the second LinerView or RelativeView, which will come on top, where you can have buttons etc
draw an image of a buttun on Canvas then use onTouchEvent in your custom view and test for the coordinates of the touch, then do something... an example for onTouchEvent here: Make certain area of bitmap transparent on touch"

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

Setting background image in a custom view

I have created a class which extends View class.
public class SplashScreen extends View
and i use it by setting contentview
View splash = new SplashScreen(this);
setContentView(splash);
I need to set background image but I can't use layout. I think I need to do canvas drawing but I don't know how to do.
protected void onDraw(Canvas canvas) {
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.LTGRAY);
// canvas.drawImage(R.drawable.background_image); (Ps: I know there is no function such as drawImage)"
canvas.drawOval(ballBounds, paint);}
If you want to just set the background you can do
public SplashScreen(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.background);
}
you can add image on canvas as:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null);
Don't you just want to change SplachScreen type to activity? Then setContentView change to whatever your layout is and if you want that splach screen appear before your application, make it first in Manifest and in splashscreen end, destroy activity and start your application menu activity. Then you won't need View class and less work finding where is problem

Canvas and drawing image in layout

I have a code where I draw image:
class Panel extends View {
public Panel(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.calvin_logo_small);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(_scratch, x-point, y-point, null);
}
}
How can I draw this image in my activity, but I don't want to change may layout. I have layout: setContentView(R.layout.main); This is possible to draw in this lauout with canvas? I have this activity and this layout have a lot of components. I only want image in place where I click with canvas. This is idea. I start application where start activity with my setContentView(R.layout.main);. After that I click on the screen and canvas draw picture in place where I clicked. This is possible to do?
Your code should work. Just override the onTouchEvent() for the view, store the co-ordinates of the touch event in field variables and invalidate() the view so that onDraw() would be called. Use the co-ordinates in onDraw() to render the image as you've already done.
To improve performance, you can cache the bitmap if it's not going to change.

Android drawing button to canvas with custom view?

How can I draw a button on top of the canvas in a custom view? (Preferably on the mid-right side) Is there something I have to call before doing the button.draw(canvas)?
public class MyClass extends View {
public Simulation(Context context) {
super(context);
pauseButton.setText("TestButton");
pauseButton.setClickable(true);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Button Pressed!");
}
});
public onDraw(Canvas canvas) {
super.onDraw(canvas);
pauseButton.draw(canvas);
}
}
Thanks for your time
You cannot insert a button into canvas. Canvas is an interface for bitmap or a bitmap buffer for a view. You can only draw other bitmap or pixels in it, not insert an object or a widget.
There are some solutions:
as Nikolay suggested, use a FrameLayout and create two layers (views), first your custom view and the second LinerView or RelativeView, which will come on top, where you can have buttons etc
draw an image of a buttun on Canvas then use onTouchEvent in your custom view and test for the coordinates of the touch, then do something... an example for onTouchEvent here: Make certain area of bitmap transparent on touch
Why do you need to draw the button yourself? Use a FrameLayout and simply have the button overlayed on your custom view.
Try this
public onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
pauseButton.draw(canvas);
canvas.restore();
}

Categories

Resources