How to achieve multitouch for whole activity? - android

I have one activity which is having linear layout and inside this layout there are several linear layouts and each linear layout is having set if buttons and text views. I want to achieve multi touch feature for whole screen means if user perform zoom in-zoom out using his finger then it should zoom in and zoom out whole screen(increase and decrease all buttons,text views size accordingly once).
How to achieve it using android 2.1?
regards,
Piks

This might give you an idea:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
//import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
public class MultitouchView extends View {
private static final int STROKE_WIDTH = 1;
private static final int CIRCLE_RADIUS = 20;
private ArrayList<PointF> touchPoints = null;
private Paint drawingPaint = null;
private boolean isMultiTouch = false;
private int pathEffectPhase = 0;
public MultitouchView(Context context) {
super(context);
initialize(context);
}
public MultitouchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(context);
}
public MultitouchView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(touchPoints.size() > 0)
{
DashPathEffect effect = new DashPathEffect(new float[] {7,7}, pathEffectPhase);
PointF midpt = null;
drawingPaint.setPathEffect(effect);
for(int index=1; index<touchPoints.size(); ++index)
{
midpt = getMidPoint(
touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
touchPoints.get(index).x,touchPoints.get(index).y);
canvas.drawCircle(
touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
1, drawingPaint);
canvas.drawCircle(
touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
CIRCLE_RADIUS, drawingPaint);
canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y,
1, drawingPaint);
canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y,
CIRCLE_RADIUS, drawingPaint);
canvas.drawLine(
touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
touchPoints.get(index).x,touchPoints.get(index).y,
drawingPaint);
canvas.drawCircle(midpt.x,midpt.y, 10, drawingPaint);
}
++pathEffectPhase;
invalidate();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int action = event.getAction() & MotionEvent.ACTION_MASK;
switch(action)
{
case MotionEvent.ACTION_DOWN:
{
invalidate();
break;
}
case MotionEvent.ACTION_POINTER_DOWN:
{
isMultiTouch = true;
setPoints(event);
invalidate();
break;
}
case MotionEvent.ACTION_POINTER_UP:
{
isMultiTouch = false;
break;
}
case MotionEvent.ACTION_MOVE:
{
if(isMultiTouch)
{
setPoints(event);
invalidate();
}
break;
}
}
return true;
}
private void initialize(Context context){
drawingPaint = new Paint();
drawingPaint.setColor(Color.RED);
drawingPaint.setStrokeWidth(STROKE_WIDTH);
drawingPaint.setStyle(Paint.Style.STROKE);
drawingPaint.setAntiAlias(true);
touchPoints = new ArrayList<PointF>();
}
public void setPoints(MotionEvent event){
touchPoints.clear();
int pointerIndex = 0;
for(int index=0; index<event.getPointerCount(); ++index)
{
pointerIndex = event.getPointerId(index);
touchPoints.add(new PointF(event.getX(pointerIndex),event.getY(pointerIndex)));
}
}
private PointF getMidPoint(float x1,float y1, float x2, float y2) {
PointF point = new PointF();
float x = x1 + x2;
float y = y1 + y2;
point.set(x / 2, y / 2);
return point;
}
}

Related

Android -Measure the Coordinates of texts with the onTouchEvent coordinates

I want to dynamically update the Coordinate values of texts stored in an array when MotionEvent.ACTION_MOVE triggers
The problem is i can manually change the Coordinates, But how to measure the Coordinates of Texts and Coordinates of Event.
package argha.paperzone.papereditor.paperviews;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import argha.paperzone.papereditor.models.PaperTextModel;
public class PaperEditorView extends View {
private Paint paint;
private List<PaperTextModel> textModelList = new ArrayList<>();
public PaperEditorView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(60);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
}
public void addText(String text) {
if (textModelList.size() == 0) {
textModelList.add(new PaperTextModel(1, 100, 100));
} else {
int lastId = textModelList.get(textModelList.size() - 1).getId();
int lastX = textModelList.get(textModelList.size() - 1).getXAxis();
int lastY = textModelList.get(textModelList.size() - 1).getYAxis();
textModelList.add(new PaperTextModel(lastId + 1, lastX, lastY + 100));
}
invalidate();
}
public void clearAll() {
textModelList.clear();
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (textModelList.size() > 0) {
for (int i = 0; i < textModelList.size(); i++) {
canvas.drawText("Text " + textModelList.get(i).getId(),
textModelList.get(i).getXAxis(),
textModelList.get(i).getYAxis(),
paint
);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int eventAction = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
textModelList.get(2).setXAxis(x);
textModelList.get(2).setYAxis(y);
}
invalidate();
return true;
}
}

Android Canvas Undo and Redo with Bitmap

I'm having problem with undo and redo operations on a canvas.
I noticed the below code works if I don't use canvas.drawbitmap in the Ondraw() method but I need to draw to bitmap so I can save canvas image and as well load image. Kindly help me.
Below is my code.
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.akinslove.drawingapp.activities.DrawingCanvasActivity;
public class DrawView extends View {
// for bitmap
private Bitmap mainBitmap;
private Canvas mainCanvas;
private Paint mainbitmapPaint;
// for canvas
private Path currentPath;
private Paint currentpathPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialiseMyComponents();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
initialiseMyComponents();
}
public DrawView(Context context) {
super(context);
initialiseMyComponents();
}
private void initialiseMyComponents() {
currentPath = new Path();
currentpathPaint = new Paint();
currentpathPaint.setColor(Color.BLACK);
currentpathPaint.setStrokeWidth(10);
currentpathPaint.setStyle(Style.STROKE);
currentpathPaint.setStrokeJoin(Join.ROUND);
currentpathPaint.setStrokeCap(Cap.ROUND);
currentpathPaint.setAntiAlias(true);
mainbitmapPaint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
for (Path p : paths){
canvas.drawPath(p, currentpathPaint);
}
canvas.drawPath(currentPath, currentpathPaint);
**//I wish to use the below line of code
canvas.drawBitmap(mainBitmap, 0, 0, mainbitmapPaint);**
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (mainBitmap == null) {
mainBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
mainCanvas = new Canvas(mainBitmap);
mainCanvas.drawColor(Color.WHITE);
}
}
float lastX;
float lastY;
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
undonePaths.clear();
DrawingCanvasActivity.IMAGEDRAWN = true;
currentPath.moveTo(x, y);
lastX = x;
lastY = y;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
currentPath.quadTo(lastX, lastY, (lastX + x) / 2, (lastY + y) / 2);
mainCanvas.drawPath(currentPath, currentpathPaint);
lastX = x;
lastY = y;
invalidate();
break;
case MotionEvent.ACTION_UP:
currentPath.lineTo(x, y);
mainCanvas.drawPath(currentPath, currentpathPaint);
// kill this so we don't double draw
paths.add(currentPath);
currentPath = new Path();
currentPath.rewind();
invalidate();
break;
}
return true;
}
// method to get bitmap
public Bitmap getMainBitmap() {
return mainBitmap;
}
// method to set bitmap
public void setMainBitmap(Bitmap mpt) {
mainBitmap = mpt;
mainCanvas = new Canvas(mainBitmap);
postInvalidate();
}
public void onClickUndo () {
if (paths.size()>0)
{
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
}
public void onClickRedo (){
if (undonePaths.size()>0)
{
paths.add(undonePaths.remove(undonePaths.size()-1));
invalidate();
}
}
}
I just found out that the line mainCanvas.drawPath(currentPath, currentpathPaint); at both case MotionEvent.ACTION_UP: and case MotionEvent.ACTION_MOVE: should not be there. It seems to redraw the path to bitmap and onto the canvas. Not sure if I speak the right android terms.

How can I rotate, zoom and drag together in an Image view, with border in Android

How can I perform rotate, zoom and drag together in an Android Imageview
For rotate, scale and rotate, try this. Worked 100%.
You can try something like this:
package com.my.test;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageViewActivity extends Activity implements OnClickListener, SurfaceHolder.Callback {
ImageView view;
final static String TAG = "TAG";
ScaleGestureDetector scaleGestureDetector;
Thread mDrawingThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*PinchImageView view = new PinchImageView(this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(view);*/
setContentView(R.layout.main);
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.pic));
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1) {
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.pic));
}
if(v.getId() == R.id.button2) {
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.penguins));
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
/**
* #author
*
*/
public class PinchImageView extends SurfaceView {
final String TAG = "TAG";
final static int HORIZONTAL_SNAP_GAP = 30;
final static int VERTICAL_SNAP_GAP = 50;
private static final int INVALID_POINTER_ID = -1;
private Drawable mIcon;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public PinchImageView(Context context) {
this(context, null, 0);
}
public PinchImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PinchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//mIcon = context.getResources().getDrawable(R.drawable.pic);
//mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
if(mPosX > HORIZONTAL_SNAP_GAP) mPosX = HORIZONTAL_SNAP_GAP;
if(mPosY > VERTICAL_SNAP_GAP) mPosY = VERTICAL_SNAP_GAP;
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int displayWidth = display.getWidth();
int displayHeight = display.getHeight();
if(mPosX + mIcon.getBounds().width() < getWidth() - HORIZONTAL_SNAP_GAP) mPosX = getWidth() - HORIZONTAL_SNAP_GAP - mIcon.getBounds().width();
if(mPosY + mIcon.getBounds().height() < getHeight() - VERTICAL_SNAP_GAP) mPosY = getHeight() - VERTICAL_SNAP_GAP - mIcon.getBounds().height();
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
/*if(mPosX > 0 || mPosY > 0) {
Animation an = new TranslateAnimation(mPosX, 0, mPosY, 0);
an.setDuration(2000);
an.setRepeatCount(-1);
//an.initialize(10, 10, 10, 10);
setAnimation(an);
an.startNow();
invalidate();
}*/
break;
}
}
return true;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
if(mIcon != null) mIcon.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
}
public void setDrawable(Drawable draw) {
mIcon = draw;
mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
invalidate();
}
}

Drag multiple images in a canvas by touching in android

How can we drag more than one image in a canvas? I have a code to drag single image. I tried for moving multiple image. But it didn't work. Is there any way to do this one? This is the code for moving one image.. Thanks in advance
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class MyImageView extends View {
private static final int INVALID_POINTER_ID = -1;
private Drawable mImage;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public MyImageView(Context context) {
this(context, null, 0);
mImage = getResources().getDrawable(R.drawable.baby1);
mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
}
public MyImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
mImage.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
invalidate();
return true;
}
}
}
I'm in search of the same thing ,so I guess is one of this 2 answers will work:
So chatgtp to find a answer just feed it your code and your problem and try it till it's what you need
or maybe store the X Y coordinates for the multiple views you want to drag and override the onTouch and check if the xy coordinates are inside any of the imageviews (this code can be found on geeksforgeeks website in java,cpp etc),if not then don't do anything else update the coordinates of the view to 'drag' the view to the new coordinates using the code from the single choice view.the only extra step is to check using xy coordinates of the imageviews if the touch event is 'inside' the view
or check Android drag two images on canvas where it gives a library that handles all of it
If you don't mind I just post my code snippet, maybe it will help you to figure out the problem.
To use this snippet just add in your layout xml file:
<com.m039.study.widgets.MultiTouch
android:layout_width = "match_parent"
android:layout_height = "match_parent"
/>
This snippet just draw circle on each finger on the view:
package com.m039.study.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import java.util.Stack;
public class MultiTouch extends View {
private static final String TAG = "m039";
public MultiTouch(Context c) {
super(c);
}
public MultiTouch(Context context, AttributeSet attrs) {
super(context, attrs);
}
static class Point {
public float x;
public float y;
public int pid = -1;
public boolean isOk = false;
}
Paint mPaintCircle = new Paint();
Paint mPaintText = new Paint();
Stack<Point> mPoints = new Stack<Point>();
{
mPaintCircle.setColor(Color.GREEN);
mPaintText.setColor(Color.RED);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Point point : mPoints) {
if (point.isOk) {
canvas.drawCircle(point.x, point.y, 50, mPaintCircle);
canvas.drawText(Integer.toString(point.pid), point.x, point.y + 50, mPaintText);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
int pid = (event.getAction() & event.ACTION_POINTER_ID_MASK) >> event.ACTION_POINTER_ID_SHIFT;
int index = (event.getAction() & event.ACTION_POINTER_INDEX_MASK) >> event.ACTION_POINTER_INDEX_SHIFT;
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
Point point = new Point();
point.isOk = true;
point.pid = pid;
point.x = event.getX(index);
point.y = event.getY(index);
mPoints.push(point);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
for (Point p: mPoints) {
int pindex = event.findPointerIndex(p.pid);
if (pindex != -1) {
p.x = event.getX(pindex);
p.y = event.getY(pindex);
}
}
invalidate();
break;
case MotionEvent.ACTION_POINTER_UP:
if (mPoints.size() >= 1) {
mPoints.pop();
}
invalidate();
break;
case MotionEvent.ACTION_UP:
default:
mPoints.clear();
invalidate();
break;
}
return true;
}
}
P.S. Please, tell me if it is helpful or not.

Adding custom View and ImageView to a single Layout

I want to add some images to the View of an app. The app uses a custom view for its normal working but I want some images along with the custom View. I have tried adding a Layout as the contenView in which I added the ImageView and the custom View, but its not comming to the screen.
Can any one give me some idea on this...
thanks in advance.
Code:
MyView view;
LinearLayout ly;
LinearLayout.LayoutParams lp;
LinearLayout.LayoutParams ImageViewlp;
lp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageViewlp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ly=new LinearLayout(this);
sketchBoard.setBackgroundColor(0);
ImageView img=new ImageView(this);
img.setBackgroundColor(R.drawable.img);
ly.addView(img,ImageViewlp);
view=new MyView(this);
ly.addView(view);
this.addContentView(ly, lp);
Edit:
img.setBackgroundResource(R.drawable.img);
I got it solved by changing img.setBackgroundColor(R.drawable.img); with the above one
You are doing this, but this result in drawing a BG color of whatever value R.Drawable.img is
img.setBackgroundColor(R.drawable.img);
should be
img.setBackgroundResource(R.drawable.img);
or better yet actually use the imageview's drawable resource and do
img.setImageResource(R.drawable.img);
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class MyImageView extends View {
private static final int INVALID_POINTER_ID = -1;
private Drawable mImage;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public MyImageView(Context context,String uri) {
this(context, null, 0);
mImage = Drawable.createFromPath(uri);
mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
}
public MyImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
mImage.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
invalidate();
return true;
}
}
}
call this through
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class sampleLocalization extends Activity {
String uri="";
ImageView img;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uri="pathname";
img.setImageResource(new touchview(context,uri));
}
}

Categories

Resources