为什么imageView的输出大小与.xml不同

时间:2016-09-29 14:26:24

标签: java android xml drag-and-drop imageview

MainActivity.java

private ViewGroup rootLayout;
private int _xDelta;
private int _yDelta;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rootLayout = (ViewGroup) findViewById(R.id.view_root);
    ImageView img = (ImageView) rootLayout.findViewById(R.id.imageView);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
    img.setLayoutParams(layoutParams);
    img.setOnTouchListener(new ChoiceTouchListener());
}



private final class ChoiceTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
        }
        rootLayout.invalidate();
        return true;
    }
}

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/cable"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="" />

</RelativeLayout>

预期输出

activity_main.xml design

结果

tested on emulator

请帮我改变imageView的大小。或者显示可以改变的编码部分。请注意,我试图更改layout_width和layout_height但没有任何改变。

3 个答案:

答案 0 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription=""
        android:scaleType="fitCenter"
        android:src="@drawable/cable" />

</RelativeLayout>

将此代码替换为您的代码

答案 1 :(得分:0)

我认为drawable文件夹中图像文件的分辨率太小,因此当它wraps_content时,它是一个小图像。尝试调整图像大小或在drawable中使用hdpi,mdpi,xdpi,xxdpi和xxxdpi文件夹。

答案 2 :(得分:0)

我找到了解决方案。 使用此代码

img.getLayoutParams().height = 1000;
img.getLayoutParams().width = 1000;

谢谢你们。