drag n drop textview in android - 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.

Related

How to get a snapshot of the view?

I am currently using a View to trace the users fingers and draw lines depending on the color picked. I have set the background image using setBackGround() but I would now like to get the background + what the user user drawn into a variable. There exists getBackGround and getForeGround, not not both. How do I achieve this? I do not want to store the image locally on the persons phone.
The code used to achieve the tracing was used from this example and the setBackground was achieved by,
dv = new DrawingView(this);
dv.setBackground(imageview.getDrawable());
Where the imageview was defined and has data in it before.
You have to pass view(name of the view), Where a view is like a relativeLayout
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap returnedBitmap = view.getDrawingCache();
imageView.setImageBitmap(returnedBitmap);

Touch event on blended layer image Android

I have a precut image i.e One base image and multiple transparent layer of the the base image.
I am trying to add the layer image on top of base image to select the layer and apply different color on it.
I have tried the following way to achieve it but not able to finish.
ImageView - I am overlapping imageview with transparent layer image. It shows the blended image but the touch event detects the finally overlapped image id always. Because I draw the image with fill parent, all the layer image is also the same
Layer Drawable - It can allow only drawable images, but my use case is to load the precut from gallery or other resource. Even I can't select layer on touch.
GPUImage library - The image is not showing full imageview.
Regards
Sathiya
Extend the View class, make the onDraw() function draw what you want to draw. The onTouchEvent() function implement the touch events. What you're trying to do isn't subsumed in any view already made but it's pretty easy to make a view. Just draw the bitmaps on the canvas in the correct order with the correct paints with the right transparency.
#Override
protected void onDraw(Canvas canvas) {
int numLayers = mLayers.size();
for (int i = 0; i < numLayers; i++) {
canvas.save();
canvas.concat(matrix);
//or
canvas.translate(rect.left,rect.top);
Bitmap b = mLayers.get(i);
canvas.drawBitmap(b, paint);
canvas.restore();
}}
You end up doing this a bunch just adding bitmaps to the canvas in whatever places and order as needed.

Android how to stretch a particular section of a Image?

I am developing a photography app in which I want to select a particular section of a photo and stretch that portion only. How to do that?
I have tried to stretch a photo using Canvas but failed to do so. Is it possible from android.graphics.NinePatch class?
Any suggestions?
You can use matrix to apply new dimension to your bitmap.
you could use setscale/postScale methods of a matrix object.
A rather ugly solution would be using a image cropping library. You can use it to temporarily crop a portion of the image and load it into another ImageView and then scaling it up.
Also, you can get things done without using that CropImageView. The idea is whenever user touches the original image view, you will be given a (x, y) at which user's finger resides. So, you can extract a bitmap image centered at (x, y) and with a given radius.
For applying a magnifier effect you could use a round/circular ImageView for showing magnified portion of the image.
Something like this:
Finally i found a solution of my problem below :
Bitmap stretchImage = Bitmap.createBitmap(w, h+progress, Bitmap.Config.ARGB_8888 );
c = new Canvas(stretchImage);
//draw top bit
c.drawBitmap(normalImage, new Rect(0,0,w,75), new Rect(0,0,w,75), null);
//draw middle bit
c.drawBitmap(normalImage, new Rect(0,75,w, 150), new Rect(0,75,w,150+progress), null);
//draw right bit
c.drawBitmap(normalImage, new Rect(0 ,150,w,225), new Rect(0 ,150+progress,w,225+progress), null);
myImage.setImageBitmap(stretchImage);

How to remove a drawn bitmap in android?

In my program, the right tube is drawn first, after that the left one is drawn too
my goal is how to remove (Clear / Hide ... ) the first drawn tube, and keep only the second one
NB : I have already checked many topics related to the canvas Drawing, but nothing works
Picture : http://i.stack.imgur.com/6lofs.png
My Method :
protected void onDraw(Canvas cv) {
Bitmap haut = BitmapFactory.decodeResource(getResources(), R.drawable.haut);
cv.drawBitmap(haut, 300, 0, null);
// WHAT SHOULD I ADD HERE TO CLEAR THE FIRST TUBE
cv.drawBitmap(haut, 0, 0, null);
}
Thanks
You can create another empty bitmap and assign it to the related image view.
it will remove the first Bitmap:
ImageView iv = findViewById(R.id.your_imageview_id);
Bitmap bmp = Bitmap.createBitmap(0 , 0 , Bitmap.Config.ARGB_8888);
iv.setImageBitmap(bmp);
iv.invalidate();
You need to redraw the background and draw just one tube. No way to 'hide' it from drawn canvas unless it is not in separate view.
You need to draw all the same images again excluding the first image and then call invalidate() which will call onDraw(). When you work at low level drawings in canvas, you need to redraw everything for each change and invalidate..

Showing a text on the picture taken from Camera and saving it in Gallery

I want to add a text on the picture taken from device camera. So I am launching my activity once Camera intent action is fired. My question is how to show a text on the picture and once user clicks on save button the picture will be saved in device's gallery with the text.Please give some idea.
Thanks
Following Snippet will give you some pointers
Here come your logic.
Here i am loading png file from drawable resource.You need to fetch it from directory.
If you want to make it more attractive Use Custom fonts by saving them in Assets/fonts
Don't directly hard-code Text you want to put rather load it from somewhere.
Use some different color.
I have manually set the Text Size .You should put some logic (Dont hard-code it)
In drawText i have harddoded Text height as 30.You should apply some logic to get Text Height.
Finally You have Background Bitmap save it.
ImageView imageView = (ImageView) findViewById(R.id.image);
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.sample).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(background);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setTextSize(50);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("Hello Image", (background.getWidth() - paint.measureText("Hello Image")) / 2, background.getHeight() - 30, paint);
imageView.setImageBitmap(background);
I think this post really helps you Link is here...
You should make use of FrameLayout and then add your imageView to it in this post given example how to set image just add textview with background transparent after it , FrameLayout displays it upon image and you can also position it(just write your textview in RelativeLayout )
Hope this explanation works for you...

Categories

Resources