Android:ImageView高度界限由于某些布局属性
我正在实现对ImageView的多点触摸缩放,效果很好,但当我将其缩小时,图像的某些部分会隐藏起来,例如隐藏在某些视图的后面或由于某些高度限制 布局代码Android:ImageView高度界限由于某些布局属性,android,image,android-imageview,multi-touch,zooming,Android,Image,Android Imageview,Multi Touch,Zooming,我正在实现对ImageView的多点触摸缩放,效果很好,但当我将其缩小时,图像的某些部分会隐藏起来,例如隐藏在某些视图的后面或由于某些高度限制 布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bggray"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivapptitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="img"
android:scaleType="fitXY"
android:src="@drawable/apptitle" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/titlecolor1">
<ImageView
android:id="@+id/ivdownloadedjpgfile"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:contentDescription="img"
/>
</ScrollView>
</LinearLayout>
类别代码
public class DownloadedImageDisplay extends Activity implements OnTouchListener{
private ImageView ivDownloadedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.staffnotationtd);
initializeVars();
showImage();
ivDownloadedImage.setOnTouchListener(this);
}
private void initializeVars()
{
ivDownloadedImage = (ImageView) findViewById(R.id.ivdownloadedjpgfile);
}
public void showImage()
{
String filePath;
Bundle extras = getIntent().getExtras();
if(extras == null) {
filePath = null;
} else {
filePath= extras.getString("JPGFileName");
}
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
ivDownloadedImage.setImageBitmap(bitmap);
}
//// Image Zooming code
private static final String TAG = "Touch";
// @SuppressWarnings("unused")
private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;
// These matrices will be used to scale points of the image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// The 3 states (events) which the user is trying to perform
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// these PointF objects are used to record the point(s) the user is touching
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN: // first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG"); // write to LogCat
mode = DRAG;
break;
case MotionEvent.ACTION_UP: // first finger lifted
case MotionEvent.ACTION_POINTER_UP: // second finger lifted
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix of points
}
else if (mode == ZOOM)
{
// pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f)
{
matrix.set(savedMatrix);
scale = newDist / oldDist; // setting the scaling of the
// matrix...if scale > 1 means
// zoom in...if scale < 1 means
// zoom out
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix); // display the transformation on screen
return true; // indicate event was handled
}
/*
* --------------------------------------------------------------------------
* Method: spacing Parameters: MotionEvent Returns: float Description:
* checks the spacing between the two fingers on touch
* ----------------------------------------------------
*/
private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/*
* --------------------------------------------------------------------------
* Method: midPoint Parameters: PointF object, MotionEvent Returns: void
* Description: calculates the midpoint between the 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);
}
/** Show an event in the LogCat view, for debugging */
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("Touch Events ---------", sb.toString());
}
}
public类downloadeImageDisplay将活动实现扩展到TouchListener上{
私有图像视图IV下载图像;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.staffnotationtd);
initializeVars();
showImage();
ivdownloadeImage.setOnTouchListener(此);
}
私有无效初始值设定项()
{
ivDownloadedImage=(ImageView)findViewById(R.id.ivdownloadedjpgfile);
}
公共void showImage()
{
字符串文件路径;
Bundle extras=getIntent().getExtras();
如果(附加==null){
filePath=null;
}否则{
filePath=extras.getString(“JPGFileName”);
}
位图位图=BitmapFactory.decodeFile(文件路径);
ivdownloadeImage.setImageBitmap(位图);
}
////图像缩放代码
私有静态最终字符串TAG=“Touch”;
//@SuppressWarnings(“未使用”)
专用静态最终浮动最小缩放=1f,最大缩放=1f;
//这些矩阵将用于缩放图像的点
矩阵=新矩阵();
矩阵savedMatrix=新矩阵();
//用户试图执行的3个状态(事件)
静态最终int NONE=0;
静态最终整数阻力=1;
静态最终整数缩放=2;
int模式=无;
//这些PointF对象用于记录用户触摸的点
PointF start=新的PointF();
PointF mid=新的PointF();
浮动oldDist=1f;
@凌驾
公共布尔onTouch(视图v,运动事件)
{
ImageView视图=(ImageView)v;
view.setScaleType(ImageView.ScaleType.MATRIX);
浮标;
dumpEvent(事件);
//在这里处理触摸事件。。。
开关(event.getAction()&MotionEvent.ACTION\u掩码)
{
case MotionEvent.ACTION_DOWN://仅首指向下
savedMatrix.set(矩阵);
set(event.getX(),event.getY());
Log.d(标记,“mode=DRAG”);//写入LogCat
模式=拖动;
打破
case MotionEvent.ACTION_UP://举起第一个手指
case MotionEvent.ACTION\u POINTER\u UP://举起食指
模式=无;
Log.d(标记“mode=NONE”);
打破
case MotionEvent.ACTION\u POINTER\u DOWN://第一个和第二个手指向下
oldDist=间距(事件);
Log.d(标记“oldDist=“+oldDist”);
如果(旧区>5f){
savedMatrix.set(矩阵);
中点(中点,事件);
模式=缩放;
Log.d(标记“mode=ZOOM”);
}
打破
case MotionEvent.ACTION\u移动:
如果(模式==拖动)
{
矩阵集(savedMatrix);
postTranslate(event.getX()-start.x,event.getY()-start.y);//在点矩阵中创建转换
}
else if(模式==缩放)
{
//收缩变焦
float newDist=间距(事件);
Log.d(标记“newDist=“+newDist”);
如果(新距离>5f)
{
矩阵集(savedMatrix);
scale=newDist/oldDist;//设置
//矩阵…如果比例>1表示
//放大…如果比例小于1表示
//缩小
矩阵。后标度(标度、标度、中x、中y);
}
}
打破
}
view.setImageMatrix(矩阵);//在屏幕上显示转换
返回true;//指示事件已处理
}
/*
* --------------------------------------------------------------------------
*方法:间距参数:MotionEvent返回:浮点描述:
*触摸时检查两个手指之间的间距
* ----------------------------------------------------
*/
专用浮动间距(MotionEvent事件)
{
float x=event.getX(0)-事件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bggray"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivapptitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="img"
android:scaleType="fitXY"
android:src="@drawable/apptitle" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/titlecolor1">
<ImageView
android:id="@+id/ivdownloadedjpgfile"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:contentDescription="img"/>
</ScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bggray"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivapptitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="img"
android:scaleType="fitXY"
android:src="@drawable/apptitle" />
</LinearLayout>
<ImageView
android:id="@+id/ivdownloadedjpgfile"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:contentDescription="img"
/>
</LinearLayout>