Hi,
Here is my layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="center"
tools:ignore="InefficientWeight" >
<ImageView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:contentDescription="@string/desc"
android:focusableInTouchMode="true"
android:scaleType="matrix" />
<ImageView
android:id="@+id/trafficview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:contentDescription="@string/desc"
android:focusableInTouchMode="true"
android:scaleType="matrix" />
<ImageView
android:id="@+id/compassview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:contentDescription="@string/desc"
android:focusableInTouchMode="true"
android:scaleType="matrix" />
<ImageView
android:id="@+id/pinview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:contentDescription="@string/desc"
android:focusableInTouchMode="true"
android:scaleType="matrix" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="@+id/mapbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/mapbutton" />
<Button
android:id="@+id/trafficbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/trafficbutton" />
<Button
android:id="@+id/compassbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/compassbutton" />
<Button
android:id="@+id/pinbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/pinbutton" />
<Button
android:id="@+id/closeapp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close" />
</LinearLayout>
</LinearLayout>
Since i have four imageviews, when i click any button respective imageview
will be activated / shown such that user can drag it across the screen,
scale and zoom it. On button click i am just using view.bringToFront(). But
i cannot use the same when user touches another imageview. Since all the
imageview width & height is *match_parent* the top most one takes is alone
is working but not views beneath it.
how to make the imageview to scroll all over the parent container along
with width & height values as *wrap_content*.
All i need is instead of button i need to use touch events to select
imageview irrespective of their z-index values.
Posted the same in forums -
http://forum.xda-developers.com/showthread.php?t=2104425&highlight=multiple+imageview
could somebody help with this
screenshot
<https://lh6.googleusercontent.com/-9ES8rtNcwEA/UPkg0naebkI/AAAAAAAAAGY/WNPdgSg30m0/s1600/TOUCH_20130118_154807.jpg>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=enpackage com.wisdom.android.multipleimageoverlays;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class ImageActivity extends Activity {
private static RelativeLayout frame;
private ImageView mapView;
private ImageView trafficView;
private ImageView compassView;
private ImageView pinView;
private Button mapButton;
private Button trafficButton;
private Button compassButton;
private Button pinButton;
private Button closeButton;
public Bitmap resourceBitmap;
// TouchListener variables
private static final String TAG = "Touch";
// Screen resolution
private static int screenHeight;
private static int screenWidth;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
private static final float MIN_ZOOM = 1.0f;
private static final float MAX_ZOOM = 3.0f;
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
boolean mapViewAdded = false;
boolean trafficViewAdded = false;
boolean compassViewAdded = false;
boolean pinViewAdded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_image);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;
frame = (RelativeLayout) findViewById(R.id.frame);
mapView = (ImageView) findViewById(R.id.mapview);
trafficView = (ImageView) findViewById(R.id.trafficview);
compassView = (ImageView) findViewById(R.id.compassview);
pinView = (ImageView) findViewById(R.id.pinview);
mapButton = (Button) findViewById(R.id.mapbutton);
trafficButton = (Button) findViewById(R.id.trafficbutton);
compassButton = (Button) findViewById(R.id.compassbutton);
pinButton = (Button) findViewById(R.id.pinbutton);
closeButton = (Button) findViewById(R.id.closeapp);
mapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mapView.bringToFront();
if (!mapViewAdded) {
setImageView(mapView, R.drawable.map);
mapViewAdded = true;
} else {
matrix.set(mapView.getImageMatrix());
}
}
});
trafficButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
trafficView.bringToFront();
if (!trafficViewAdded) {
setImageView(trafficView, R.drawable.traffic);
trafficViewAdded = true;
} else {
matrix.set(trafficView.getImageMatrix());
}
}
});
compassButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
compassView.bringToFront();
if (!compassViewAdded) {
setImageView(compassView, R.drawable.compass);
compassViewAdded = true;
} else {
matrix.set(compassView.getImageMatrix());
}
}
});
pinButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pinView.bringToFront();
if (!pinViewAdded) {
setImageView(pinView, R.drawable.pin);
pinViewAdded = true;
} else {
matrix.set(pinView.getImageMatrix());
}
}
});
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout view = (RelativeLayout) findViewById(R.id.frame);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache(true);
try {
File mediaStorageDir = new File(PictureSaver
.getOutputMediaFileUri(
PictureSaver.MEDIA_TYPE_IMAGE,
PictureSaver.IMAGE_SCREEN_SHOT).getPath());
FileOutputStream fos = new FileOutputStream(mediaStorageDir);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
view.setDrawingCacheEnabled(false);
}
ImageActivity.this.finish();
}
});
}
public class ImageOnTouch implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
v.bringToFront();
matrix.set(view.getImageMatrix());
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float newX = event.getRawX();// or +/- the current width of
// the element under drag
float newY = event.getRawY();
if ((newX <= 0 || newX >= screenWidth)
|| (newY <= 0 || newY >= screenHeight))
break;
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
// limitZoom(matrix);
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
}
private void limitZoom(Matrix m) {
float[] values = new float[9];
m.getValues(values);
float scaleX = values[Matrix.MSCALE_X];
float scaleY = values[Matrix.MSCALE_Y];
if (scaleX > MAX_ZOOM) {
scaleX = MAX_ZOOM;
} else if (scaleX < MIN_ZOOM) {
scaleX = MIN_ZOOM;
}
if (scaleY > MAX_ZOOM) {
scaleY = MAX_ZOOM;
} else if (scaleY < MIN_ZOOM) {
scaleY = MIN_ZOOM;
}
values[Matrix.MSCALE_X] = scaleX;
values[Matrix.MSCALE_Y] = scaleY;
m.setValues(values);
}
/** Show an event in the LogCat view, for debugging */
@SuppressWarnings("deprecation")
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private void setImageView(ImageView view, int drawable) {
view.setImageDrawable(getResources().getDrawable(drawable));
resourceBitmap = BitmapFactory.decodeResource(getResources(), drawable);
float centeredWidth = (float) (screenWidth / 2)
- (resourceBitmap.getWidth() / 2);
float centeredHeight = (float) (screenHeight / 2)
- (resourceBitmap.getHeight() / 2);
matrix = new Matrix();
matrix.postTranslate(centeredWidth, centeredHeight);
view.setImageMatrix(matrix);
view.setOnTouchListener(new ImageOnTouch());
frame.removeView(view);
frame.addView(view);
}
}