Combine one image overlaying another image - android

I have two images, Image A which is the big background at the back and image B that is a small icon that will be merging on top of image A.
How it works
The user takes a photo from the camera and this photo will be Image A.
The user selects the icon from the layout and that will be Image B.
After selecting the image for image B, the user can move image B around the layout to adjust the position where the image B will overlay on top of image A.
After which the user pressed save, the canvas will merge two image, B on top of A, with the position the user wants and save it to SD card.
Problem
I have managed to get the image B to move around the layout but I do not know how to get it to merged at the position to the image A.
This is what i did to get the image B to move around the layout.
img_additionalImage = (ImageView) findViewById(R.id.img_additionalImage);
img_additionalImage.setOnTouchListener(new OnTouchListener()
{
#SuppressLint("NewApi")
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
isImageMoving = true;
break;
case MotionEvent.ACTION_MOVE:
if (isImageMoving)
{
x = event.getRawX() - img_additionalImage.getWidth() / 2;
y = event.getRawY() - img_additionalImage.getHeight() / 2;
img_additionalImage.setX(x);
img_additionalImage.setY(y);
}
break;
case MotionEvent.ACTION_UP:
isImageMoving = false;
break;
}
return true;
}
});
I do not know how to merge two images together with position the user chose.

if you have use RealtiveLayout or LinearLayout as a parent layout of this 2 imageview than you can capture that view by this way..
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));
Where view is your View. The 95 is the quality of the JPG compression. And the file output stream is just that.
what does setDrawaingCacheEnabled?
Enables or disables the drawing cache. When the drawing cache is
enabled, the next call to getDrawingCache() or buildDrawingCache()
will draw the view in a bitmap. Calling draw(android.graphics.Canvas)
will not draw from the cache when the cache is enabled. To benefit
from the cache, you must request the drawing cache by calling
getDrawingCache() and draw it on screen if the returned bitmap is not
null.
Enabling the drawing cache is similar to setting a layer when hardware
acceleration is turned off. When hardware acceleration is turned on,
enabling the drawing cache has no effect on rendering because the
system uses a different mechanism for acceleration which ignores the
flag. If you want to use a Bitmap for the view, even when hardware
acceleration is enabled, see setLayerType(int, android.graphics.Paint)
for information on how to enable software and hardware layers.
from Android Docs

Related

ImageView Pinch Scaling

Right now I have a layout that contains two imageviews, one in the background and one in the front that is a icon. Both of them will merge together as a single image and save it to sd card. The image of the icon can be move around before the user save it to sd card.
Now what i require is to allow the user to pinch the image of the icon so they could scale the image evenly without losing its proportion.
This is how i allow my icon image to move around in my onCreate class.
case MotionEvent.ACTION_MOVE:
if (isImageMoving)
{
x = event.getRawX() - img_additionalImage.getWidth();
y = event.getRawY() - img_additionalImage.getHeight();
img_additionalImage.setX(x);
img_additionalImage.setY(y);
}
break;
I want the user to freely scale the icon image to the desired size then let them save it to sd card.

Simple ImageView zooming

I'm trying to build a screen that shows a bunch of thumbnails at the bottom along with a full size version of the photos.
The thumbnails are (indirect) children of a HorizontalScrollView and the full size pictures are added as ImageViews of Fragments in a ViewPager.
What is the simplest way of zooming in on an ImageView? Zooming will use a fixed factor.
I've been unable to locate the Android Gallery source code, which might help me.
public boolean onDoubleTap(MotionEvent event) {
Log.e("test", "on double tap; event: "+ event);
Matrix m = new Matrix();
m.setScale(1.25f, 1.25f);
clickedImageView.setImageMatrix(m);
clickedImageView.invalidate();
clickedImageView.refreshDrawableState();
Log.e("test", "set image matrix after double tap event");
return true; // indicate event was handled
}
That code looks like it should work, just make sure you set
android:scaleType="matrix"
in the xml for the ImageView, or
clickedImageView.setScaleType(ImageView.ScaleType.MATRIX);
if you want to do it in the code. Otherwise the matrix you have set will not be applied.

Android: Creating shaped button

How can I create a custom button like this?
It should be just clickable area not a real button.
I use a crapload of irregular shaped buttons on my app, and to change the "hot zone" or "clickable area" of the button, I just use the Bitmap.getPixel() method to check for alpha on the image used. If the method returns 0, then don't perform click event.
Example:
(1) Create your button as usual, whichever way you would like.
(2) Define a Bitmap and assign to it the same image drawable used for the button.
(3) Get the X and Y coordinates of the touch or click action.
(4) Pass the coordinates to the .getPixel(x,y) method.
Sample Code:
// ** Declare your Bitmap somewhere **
final Bitmap TheBitmap;
TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage);
// ** My onTouch code **
public boolean onTouch(View v, MotionEvent event) {
int eventPadTouch = event.getAction();
switch (eventPadTouch) {
case MotionEvent.ACTION_DOWN:
if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.
if (TheBitmap.getPixel(iX,iY)!=0) {
// * A non-alpha area was clicked, do something
}
}
return true;
}
return false;
}
The event.getX() and event.getY() simply give you the coordinates of where you touched the button.
** The above sample is to guide you in the correct direction. There are some checks to add to the code to assure no errors occur.
ceate using your own canvas or make a image using photoshop like this and then using Bitmap.createScaledBitmap scale it according to dimension of your button and hence you will get this button.
using canvas you have to write more code just do this it will work fine
Simply just create an image like that and you can use either ImageView or Button w/o text, and implement an OnClickListener. It just works!
save that as a png and and put it in your drawables folder. Then in your xml use something like this
<Button
android:height="wrap_content"
android:width="wrap_content"
android:background="#drawable/your_png"
/>
I am not 100% positive that the corner cut out is going to work properly. That corner area that is gone may end up being clickable. If that is the case and if you don't want it to be then you'll have to slice your picture in half somewhere create two buttons that you can set next to each other to make up that shape and use the same click listener for both. From the users perspective it will still seem like one button.

drag n drop textview in android

I want to drag n drop the text view on image in android 2.X
please , look at this image below.
Here, "Blue" color represents the ViewGrop , "White" is ImageView and the image is set on the ImageView.
the text written "Fashion" is a TextView.This is how i have implement the structure. Now i want to allow the user to select the TextView and drag it to any part of the image and then drop it over the image at desired position.
As of now for a reference i tried to refer the following url but whenever i use TextView instead of Button things are getting abnormal.
link text
Can anyone give me the road map or example to get it done ?
Instead, try getting your ImageView as a canvas to draw into.
ImageView CanvasView = (ImageView) findViewById(R.id.fashion_pic)
From here you can create your own Bitmap and re-draw it after a TouchEvent. The following snippet contains a necessary work-around for a bug in 2.1 (or 2.2, I can't remember):
public void redrawImage() {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.fashion_pic);
Bitmap proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(proxy);
//Here, we draw the background image.
c.drawBitmap(bm, new Matrix(), null);
//Here, we draw the text where the user last touched.
c.drawText("Fashion", someGlobalXvariable, someGlobalYvariable, somePaint);
CanvasView.setImageBitmap(proxy);
}
Here, you have created a canvas which already has your picture as the background and paints text at an x and y variable. Now, just set an OnTouchListener:
CanvasView.setOnTouchListener( new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
someGlobalXvariable = e.getX();
someGlobalYvariable = e.getY();
redrawImage();
return true;
}
});
As you can see, the listener automatically updates your image when it is touched, and the text will follow the user's finger. Since you're using a Bitmap as your canvas, you can also add support for saving/loading an image if your app wants to have that kind of functionality.
Edit: It may be more performance-friendly if you move lines 1-3 from redrawImage() and place them elsewhere, setting those objects as global objects. I'll let you tinker with that.

Is it possible to move components around the screen using the standard android apis?

I would like to produce an android user interface which allows the user to move added components/widgets around the screen by selecting them and then dragging them around.
Is this possible using the standard android apis?
Yes. It depends what you are trying to achieve.
It can be done using the standard APIs, but this functionality is not part of the standard APIs. That is, there is no widget.DragOverHere() method unless you write one.
That said, it would not be terribly complicated to do. At a minimum, you would need to write a custom subclass of View and implement two methods: onDraw(Canvas c) and onTouch(MotionEvent e). A rough sketch:
class MyView extends View {
int x, y; //the x-y coordinates of the icon (top-left corner)
Bitmap bitmap; //the icon you are dragging around
onDraw(Canvas c) {
canvas.drawBitmap(x, y, bitmap);
}
onTouch(MotionEvent e) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
//maybe use a different bitmap to indicate 'selected'
break;
case MotionEvent.ACTION_MOVE:
x = (int)e.getX();
y = (int)e.getY();
break;
case MotionEvent.ACTION_UP:
//switch back to 'unselected' bitmap
break;
}
invalidate(); //redraw the view
}
}
To some degree. It depends on what degree of freedom you want to give the user. One solution that comes to mind is a TableLayout with predefined cells. A user can then tap and drag components. When dragging a component you'll just want to draw an image of it under the user's finger, but when they release, remove the component from it's previous parent and add it to the new parent using the ViewGroup add/remove methods.
While you can programmatically shift Views around, it would be difficult/impossible to switch layouts on the fly as far as I can see.
The way I have done this is to have an absolute layout and then just update the object position as it is dragged. You have to implement dragging yourself. If the widgets have a natural order (a toolbar where you want to be able to drag buttons around) you can stack an absolute layout on top of the toolbar and when the drag starts, you add it to the absolute layout and when it finishes, you add it back to the original layout in the new position.

Categories

Resources