【问题标题】:Position shape at exact spot on ImageView将形状定位在 ImageView 上的确切位置
【发布时间】:2016-06-23 14:22:33
【问题描述】:

我无法让元素在 Android Studio 中的布局上正确定位。我有一个带有包含数字 2 的 png 背景的 ImageView。我有两个视图,每个视图都包含一个小圆圈。我希望圆圈始终在数字二的每个尖端对齐。我可以在一台设备上正确对齐它们,但在另一台设备上对齐会发生变化。是否有可能使这些在每个设备中始终正确对齐?

这是我想要的样子: http://i.stack.imgur.com/v7nsg.png

这是我计算点位置之一的代码:

    circle2 = findViewById(R.id.circle2);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int heightPixels = metrics.heightPixels;
    int widthPixels = metrics.widthPixels;

    //dimensions of background image: 547x839

    dot2H = (heightPixels / 547) * 70;
    dot2W = (widthPixels / 829) * 70;
    dot2Hposition = (heightPixels / 829) * 500;
    dot2Wposition = (widthPixels / 547) * 300;
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) circle2.getLayoutParams();
    params.height = dot2H;
    params.width = dot2H;
    params.leftMargin = dot2Wposition;
    params.topMargin = dot2Hposition;
    circle2.setLayoutParams(params);

这是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="net.beauvine.animationtest.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"

    android:src="@drawable/two"
    android:id="@+id/imageView"
    android:padding="10dp"
    android:layout_alignParentEnd="false"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true" />

<RelativeLayout
    android:id="@+id/dotLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center"
    >

    <View android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/circle2"
        android:background="@drawable/circle"
        android:clickable="true"
        android:alpha=".8"
        />
    <View android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/circle"
        android:background="@drawable/circle"
        android:clickable="true"
        android:alpha=".8"
        />

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-studio


    【解决方案1】:

    Android 用于大量屏幕尺寸和密度各不相同的设备。因此,很难通过 xml 布局做你想做的事。但是您可以使用具有固定视图关系的布局,因此它看起来与您想要的相似,但这不是一个好方法。

    这个例子可以给你一把钥匙。此布局显示 4 个视图 屏幕的确切位置总是。

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
    
                <View
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@android:color/white" />
            </FrameLayout>
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
    
                <View
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@android:color/white" />
            </FrameLayout>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
    
                <View
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@android:color/white" />
            </FrameLayout>
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
    
                <View
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@android:color/white" />
            </FrameLayout>
        </LinearLayout>
    
    </LinearLayout>
    

    类似这样的:

    如果您将在自定义视图中同时绘制背景(带有数字 2)和圆圈,那就更好了。这样您就可以知道所有对象的确切大小和坐标,并且可以正确放置它们。

    自定义视图中的绘图示例:

    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView(this));
        }
    
        public class MyView extends View {
            public MyView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDraw(canvas);
    
                int x = getWidth();
                int y = getHeight();
                int radius;
                radius = 100;
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.BLACK);
                canvas.drawPaint(paint);
    
                paint.setColor(Color.WHITE);
                canvas.drawCircle(x / 2, y / 2, radius, paint);
    
                paint.setColor(Color.WHITE);
                canvas.drawCircle(x / 3, y / 3, radius, paint);
            }
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我一直在尝试您的自定义视图建议,它似乎让我更接近我的目标。但是,我之前没有澄清的一件事是,这些点是在您触摸它们时会产生动画的按钮。我该如何合并它?在您的示例中,您在画布上绘制了圆圈。不幸的是,这些不能是按钮。
    • 您需要创建特定的对象来绘制自身并处理触摸动作(如视图)。你会编程类似游戏的东西吗?在这种情况下,您可以尝试使用其中一种免费游戏引擎。它们都支持自定义绘图、对象动画、必要的交互等。
    • 感谢您的帮助!我最终为每个点制作了另一个自定义视图,并将它们制作成按钮。您的解决方案绝对让我走上了正确的道路。
    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多