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

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".

Related

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/

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

Getting shadows/outlines/shapes of icons

I was wondering if it is possible to get shadows/shapes of icons on home screen in android? I know when you add some custom icon to your homescreen and then move it around you get outlines of that icon. ( at least in samsungs TouchWiz 4+ and API 11+ ). So is there a way to obtain shapes and locations of icons on homescreen?
Here is a short sample of what you could do:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView view = ((ImageView) findViewById(R.id.img_outline));
view.setImageBitmap(createOutline(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)));
view.setColorFilter(Color.RED);
}
private Bitmap createOutline(Bitmap src) {
Paint p = new Paint();
p.setMaskFilter(new BlurMaskFilter(10, Blur.OUTER));
return src.extractAlpha(p, null);
}
}
Is that what you had in mind? (Note that if you have to align the blurred image with the original you should pass an int array of length two to extractAlpha. The array will contain the offsets you should use to align the outline.)

Android add flag on button click

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.

how can I repeat a background image according to the text size (dynamically coming by parsing) in android

I am facing a problem in my android app. I have to dynamically find the height of the text description coming from parsing the xml and show it on an imageview(background image).
I have to repeat the backround image by finding the total height of the text.
So, Please help me that how can we find the text height dynamically and repeat the background image according to that height.
Thanks.
Since there is no answer for this question.
I tried and almost did it in the following way:
public class MainActivity extends Activity
{
String description="hello how r u this project is meant for android to calculate " +
"the size of text and can dyanamically set the image height according to the text";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int length_count=description.length();
int window_width=getWindowManager().getDefaultDisplay().getWidth();
int lines=(length_count*14)/(window_width-30);
Log.v("length",""+length_count);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.project_dess_box_mid);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
LinearLayout layout = (LinearLayout)findViewById(R.id.linear1);
TextView tv=(TextView)findViewById(R.id.text01);
tv.setLines(lines);
tv.setText(description);
layout.setBackgroundDrawable(bitmapDrawable);
}
}
I posted this as it may be helpful for others too.

Categories

Resources