简体   繁体   English

设备屏幕上的中心图像

[英]Center image on device screen

I've a simple square image for test the drag zoom & rotation system.我有一个简单的方形图像,用于测试拖动缩放和旋转系统。 All work fine but I've problem on center the image in the center of device as show in picture (the same thing of matrix images... I've read a lot of post here but not have found a solution).一切正常,但我在将图像居中放置在设备中心时遇到了问题,如图所示(矩阵图像也是如此......我在这里阅读了很多帖子,但没有找到解决方案)。 With the code below the image is on the left corner.使用下面的代码,图像位于左上角。

图像测试 设备

the xml file... 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>

and the activity和活动

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);
}
}

In RelativeLayout , add android:layout_centerInParent="true" .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>

Since you are assigning your image view a new set of layout params programmatically:由于您正在以编程方式为图像视图分配一组新的布局参数:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 800);
layoutParams.topMargin = 0;
layoutParams.bottomMargin = 0;
layoutParams.rightMargin = 0;
layoutParams.leftMargin = 0;

fullImage.setLayoutParams(layoutParams);

Every constraint on the image view in this layout that you previously used (inside .xml) is reset to default and you must include the constraints again programatically.您之前使用的此布局中图像视图上的每个约束(在 .xml 中)都将重置为默认值,您必须再次以编程方式包含这些约束。 For example in case of your code, you forgot to add the centerInParent constraint, so including centerInParent and changing width and height to match parent:例如,在您的代码中,您忘记添加 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);

Results in what was expected:结果符合预期:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM