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.
Related
How to place a picture at some user input specific pixels on the screen in android. if a user taps at a certain point than an image should be pasted on that point. my main concern is not getting the pixels but putting an image within that set of pixels (I am having a set of pixels within that i have to re-scale the image and than place it at that position )
as told by Ran hassid first find the touch coordinates on screen by using
view.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View view, MotionEvent motionEvent) {
motionEvent.getX()
motionEvent.getY()
return false; } });
and then follow this answer to show image at obtained cordinates by above code-
link
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
I need to detect touches on red spots on image bellow, so after touch on spot I can make action.
I guess I need to detect touch coordinates, and check if it matches red spot. But how can I detect red spot position? Is it possible?
Note: I can choose image format if it helps, but probably we need work with bitmap.
I need to get dot position programmatically. I receive picture, I need to handle touch event's on dots. But I can receive different image with different dots positions
Thanks for all thoughts!
The simplest solution I see is:
1- Get the imageView size as it might change depending on device screen size and/or resolution.
int IVwidth = imageView.getWidth();
int IVHeight = imageView.getHeight();
2- Get the Bitmap width/height and then find the mathmatical relation (ratio) between bitmap width and imageView width,
Simply:
float HeightRatio = (float)bm.getHeight() / (float)iv.getHeight();
float WidthRatio = (float)bm.getWidth() / (float)iv.getWidth();
lets say we have a bitmap of (800*600) size and an imageView of (400*300) size
this would give you a ratio of (2.0) for height and width.
3- Now when the event ImageViewTouched is raised, you can get the touch coordinates quite easy as the event will deliver them to you. Notice that catching the touch event is quite easy and simply can be implemented by adding this to the view;
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_UP)
{
int x = arg1.getX();
int y = arg1.getY();
int XonImage = x * WidthRatio;
int YonImage = Y * HeightRatio;
if(bm.getPixel(XonImage, YonImage) == Color.RED)
{
// BINGOOOOOOO, do your magic here
}
}
return false;
}
});
For anymore info please ask without hesitation.
Cheers
I would use a FrameLayout. The view hierarchy should be:
<FrameLayout>
<ImageView vineyard>
<LinearLayout dots_holder>
<ImageView dot1/>
<ImageView dot2/>
<ImageView dotn/>
</LinearLayout>
</FrameLayout>
Your dots will be overlayed on the vineyard image and you can add an OnClickListener to each one. For positioning and dimensions of each dot ImageView, I would do some dynamic calculation in your code.
There's so many ways. You might be able to use SemperFly's method. There's also onTouchListeners for views, which would give you the coordinates. You may have to do translate the coordinates to handle different screen densities.
You also may be able to divide the image into a grid, which may simplify or avoid the math. Another option would be to chop the image into tiles. You can also use something like a RelativeLayout.
How accurate do you need the touch detection?
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.
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.