我有一个简单的正方形图像,用于测试拖动缩放和旋转系统。 一切正常,但我在将图像居中显示在设备中央时遇到了问题(矩阵图像相同……我在这里读了很多文章,但没有找到解决方案)。图片下方的代码在左上角。
xml文件...
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.br1dev.myapplication.MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fullImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:contentDescription="@android:string/untitled"
android:scaleType="centerInside"
app:srcCompat="@drawable/f1" />
</RelativeLayout>
和活动
public class MainActivity extends AppCompatActivity
{
ImageView fullImage;
float scalediff;
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
private float oldDist = 1f;
private float d = 0f;
private float newRot = 0f;
@SuppressLint("ClickableViewAccessibility")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 800);
layoutParams.topMargin = 0;
layoutParams.bottomMargin = 0;
layoutParams.rightMargin = 0;
layoutParams.leftMargin = 0;
fullImage.setLayoutParams(layoutParams);
fullImage.setOnTouchListener(new View.OnTouchListener()
{
RelativeLayout.LayoutParams parms;
int startwidth;
int startheight;
float dx = 0, dy = 0, x = 0, y = 0;
float angle = 0;
@Override
public boolean onTouch(View v, MotionEvent event)
{
final ImageView view = (ImageView) v;
((BitmapDrawable) view.getDrawable()).setAntiAlias(true);
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
parms = (RelativeLayout.LayoutParams) view.getLayoutParams();
startwidth = parms.width;
startheight = parms.height;
dx = event.getRawX() - parms.leftMargin;
dy = event.getRawY() - parms.topMargin;
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f)
{
mode = ZOOM;
}
d = rotation(event);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) (x - dx);
parms.topMargin = (int) (y - dy);
parms.rightMargin = 0;
parms.bottomMargin = 0;
parms.rightMargin = parms.leftMargin + (5 * parms.width);
parms.bottomMargin = parms.topMargin + (10 * parms.height);
view.setLayoutParams(parms);
} else if (mode == ZOOM) {
if (event.getPointerCount() == 2)
{
newRot = rotation(event);
float r = newRot - d;
angle = r;
x = event.getRawX();
y = event.getRawY();
float newDist = spacing(event);
if (newDist > 10f)
{
float scale = newDist / oldDist * view.getScaleX();
if (scale > 0.6)
{
scalediff = scale;
view.setScaleX(scale);
view.setScaleY(scale);
}
}
view.animate().rotationBy(angle).setDuration(0).setInterpolator(new LinearInterpolator()).start();
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) ((x - dx) + scalediff);
parms.topMargin = (int) ((y - dy) + scalediff);
parms.rightMargin = 0;
parms.bottomMargin = 0;
parms.rightMargin = parms.leftMargin + (5 * parms.width);
parms.bottomMargin = parms.topMargin + (10 * parms.height);
view.setLayoutParams(parms);
}
}
break;
}
return true;
}
});
}
private void init()
{
fullImage = findViewById(R.id.fullImage);
}
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);
}
private float rotation(MotionEvent event)
{
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
}
答案 0 :(得分:0)
在RelativeLayout
中,添加android:layout_centerInParent="true"
。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:scaleType="centerInside"
android:layout_centerInParent="true" />
</RelativeLayout>
答案 1 :(得分:0)
由于要以编程方式为图像视图分配一组新的布局参数:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 800);
layoutParams.topMargin = 0;
layoutParams.bottomMargin = 0;
layoutParams.rightMargin = 0;
layoutParams.leftMargin = 0;
fullImage.setLayoutParams(layoutParams);
此布局中以前使用的图像视图上的所有约束(在.xml中)都被重置为默认值,并且您必须再次以编程方式包括这些约束。例如,在您的代码中,您忘记添加centerInParent约束,因此包括centerInParent并更改宽度和高度以匹配父对象:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layoutParams.topMargin = 0;
layoutParams.bottomMargin = 0;
layoutParams.rightMargin = 0;
layoutParams.leftMargin = 0;
fullImage.setLayoutParams(layoutParams);
得到预期的结果: