Android add flag on button click - android

I have a button and I want when I click on it the flag.png image gets created and I place it anywhere on the canvas
this is my flag button code
// the flag button
Button flag = (Button) findViewById(R.id.btnFlag1);
flag.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
flag(R.drawable.flag1);
}
});
and this is my function
public void flag(int resourceID) {
Bitmap flagBitmap = BitmapFactory.decodeResource(
getResources(), resourceID);
Canvas c = new Canvas(flagBitmap);
c.drawBitmap(flagBitmap, null, null);
}
nothing happens atm ..
the flags are circle and I have them as buttons, and what I want is them to be added when each button is clicked its flag is inserted and I can put it where I want
Like a smiley on any photo editing app

I don't understand why you mentioned using an imageview in your question while what you're using is canvas to draw a bitmap image. If you want to do it the imageview-way, here's some code to help you out:
ImageView pic = (ImageView) findViewById(R.id.imageview);
Bitmap img=BitmapFactory.decodeResource(getResources(),R.drawable.flag1);
pic.setImageBitmap(img);
Make sure to declare your imageview in the xml file. Feel free to comment if there are any further questions or if I got your question wrong.
Edit:
Matrix matrix = new Matrix();
matrix.reset();
matrix.postTranslate(x, y);
pic.setScaleType(ScaleType.MATRIX);
pic.setImageMatrix(matrix);
Instead of x and y, insert the x and y co-ordinates where you want to place the flag.

Related

How to do image blur on touch (progressively)?

I want to blur a portion of bitmap and set into image view. But I am not getting any reference of the same.
Took me 40 seconds to find the solution in stackoverflow, Please use search :)
Use the blur from here:
https://stackoverflow.com/a/10028267/5618671
(Make sure to +1 Yahel, the user that posted the blur solution)
Get your Bitmap (can get it from your existing imageView), Call the fast blur when imageView has been clicked, and set the imageView with the new blurred Bitmap :
example
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GET BITMAP FROM IMAGEVIEW
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmapFromImageView = drawable.getBitmap();
Bitmap blurredBitmap = fastblur(bitmapFromImageView, BITMAP_SCALE, BLUR_RADIUS)
imageView.setImageBitmap(blurredBitmap);
}
});
P.S Make sure imageview is declared final:
final ImageView imageView;

Android merge two images, 1 on top of another [duplicate]

This question already has an answer here:
Combining 2 Images overlayed
(1 answer)
Closed 5 years ago.
I need to merge two images, one on top of another. I have the first image (background.png) where it has an transparent portion. I want to put another image (image.png) on top of the background.png.
However for the final image that gets created, I only want part of the image.png that overlaps with the transparent portion of the bakground.png to be shown, the rest of the new image will show the background.
Here's my code for merging the image, but I'm not sure how to go from here. Thanks.
public class MainActivity extends ActionBarActivity {
private ImageView collageImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collageImage = (ImageView)findViewById(R.id.imageView3);
Button combineImage = (Button)findViewById(R.id.combineimage);
combineImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bigImage = BitmapFactory.decodeResource(getResources(), R.drawable.multiple);
Bitmap smallImage = BitmapFactory.decodeResource(getResources(), R.drawable.multipletwo);
Bitmap mergedImages = createSingleImageFromMultipleImages(bigImage, smallImage);
collageImage.setImageBitmap(mergedImages);
}
});
}
private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage){
Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, 10, 10, null);
return result;
}
}
Try using 2 imageviews that overlap in the layout. Maybe use a Relative layout to position them how you want. Then you will set an image for each iamgeview and they will be "merged".

Clickable image layers with transparent background

I have two images with transparent background.
I added both to the layout view.
I need to assign an onClickListener to each image.
The problem is that only the topmost image is firing the click event (is like if the top most image cover the rest and don't care about its transparent background). Both images are .png with transparent background
Here is the code:
ImageView img1, img2;
RelativeLayout l = (RelativeLayout)findViewById(R.id.layout1);
bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
img1 = new ImageView(this);
img1.setImageBitmap(bm1);
l.addView(img1);
img2 = new ImageView(this);
img2.setImageBitmap(bm2);
l.addView(img1);
img1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img1 is clicked
}
});
img2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img2 is clicked
}
});
In this example, only img2 is firing the event.
I need that both img2 and img1 can fire their events.
Thanks in advance for your help.
If the transparent background of one image covers the second, then the first will respond as being clicked, because the transparent background is counted as part of the image.
Perhaps the simplest answer would be a little bit of photo-editing to remove some of the transparent background. Crop the images a bit.

controll bitmap image manually

I,m taking my first step into movement of bitmaps. From bits on the internet i,ve created this simple code. The bitmap moves across the screen from top left to top right it goes off the screen and back on at 0,0. What i want to do is add a button or method to manually move the image. I,m only using this single class and have noticed it does not use the main_activity xml Or does it?? If someone could show me on this 1 direction i can duplicate for the other directions. If youd like to add code so doesnt go off screen would be a bonus
public class MainActivity extends Activity {
int x=0;
int y=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this)); }
private class myView extends View{
public myView(Context context) {
super(context); }
#Override
protected void onDraw(Canvas canvas) {
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.richinch);
if (x < canvas.getWidth()){x +=10;}
else {x=0;}
canvas.drawBitmap(myBitmap, x, y, null);
invalidate();
}}}
Ive added this to the code and read a little on OnTouch listener. How would i add that to the region or Rectangle this would be very helpfull so effectively i,m using the Bitmap as a button if was button id know with onclick, Basicall im trying to make 2 Bitmap buttons to move the image Left Right for now.Eventually all directions. Please use names im using unless creating summit eg int etc
Paint green = new Paint();
green.setColor(Color.RED);
green.setStyle(Paint.Style.FILL);
////creating the shape////
Rect rect= new Rect();
rect.set(0, 0,x+50, x+50);
canvas.drawRect(rect,green);
Region region = new Region(0, 950, 100, 1030);
I am not exactly sure what you want to achieve, but if you would like to make an animation, avoid using onDraw() and just let ObjectAnimator do the "moving" for you. Here's a detailed tutorial on it.
The minimum code you need:
ObjectAnimator animation = ObjectAnimator.ofFloat(yourObject, "x", xDest);
animation.setDuration(500); // milliseconds
animation.start();
You are not using any xml
this part here:
setContentView(new myView(this)); is where you would add your xml file setContentView(R.layout.mainxml)
If you want to move around a bitmap with the touch of your finger check out these tutorials. They do exactly this and you will learn to use a SurfaceView
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-i/
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-ii/
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-iii/

Resize Imageview and fitXY

I am resizing an ImageView and auto fitting the image to it, I need this for zoom in and out for image, this is the way i wish to implement this because the image is already 2048x2048 and I cant re-size it. Bu the code only works once for some reason, second click does not do anything. If you have other ideas or suggestions I would love to hear them. Thank you.
ImageView map = (ImageView) findViewById(R.id.imageViewMap);
Bitmap mapIm = BitmapFactory.decodeResource(getResources(),R.drawable.myMap);
map.setImageBitmap(mapIm); //first view
zoomIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
map.getLayoutParams().height = counter*5000 ;
map.getLayoutParams().width = counter*5000;
BitmapDrawable bmd = new BitmapDrawable(mapIm);
map.setImageDrawable(bmd);
bmd = null;
counter++;
}
});
For zooming your image view
imageview.setScaleType(ScaleType.FIT_XY);
or you can try the following link hope it may help you
zoomableimageview
try this hope it may help you
example zoom

Categories

Resources