如何将动画固定在不同的DPI上?

时间:2019-07-08 23:08:59

标签: android user-interface dpi

我有一个动画,它将png图像移到顶部,然后停在特定位置

问题在于它会停在具有不同DPI的不同设备上的不同位置

enter image description here

项目结构

enter image description here

项目链接: https://github.com/anggarisky/SplashtoHomeAngga

XML:

bganim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    >

    <translate
        android:fromYDelta="0%"
        android:toYDelta="30%p"
        android:duration="800"
        />

</set>

cloveranim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:startOffset="300"
    >


    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="800"
        />


</set>

frombottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:startOffset="1000"
    >

    <translate
        android:fromYDelta="10%p"
        android:toYDelta="0%"
        android:duration="800"
        />

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="600"
        />

</set>

acitivty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/clover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="272dp"
        android:elevation="6dp"
        android:src="@drawable/clover"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/textsplash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="300dp"
        android:elevation="6dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Good Morning"
            android:textColor="#FFF"
            android:textSize="24sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mr Panda Risky"
            android:textColor="#FFF"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/texthome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="28dp"
        android:elevation="6dp"
        android:gravity="left"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Explore"
            android:textColor="#FFF"
            android:textSize="24sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Simply makes design good"
            android:textColor="#FFF"
            android:textSize="16sp" />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/menus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="240dp"
        android:elevation="6dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.726"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="70dp"
            android:orientation="horizontal">


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="50dp"
                android:gravity="center"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:src="@drawable/bitcon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="BitCoin"
                    android:textColor="#0E1843"
                    android:textSize="22sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="50dp"
                android:gravity="center"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:src="@drawable/bellcon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Bells"
                    android:textColor="#0E1843"
                    android:textSize="22sp" />

            </LinearLayout>


        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="70dp"
            android:orientation="horizontal">


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="50dp"
                android:gravity="center"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:src="@drawable/bookcon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Books"
                    android:textColor="#0E1843"
                    android:textSize="22sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="50dp"
                android:gravity="center"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:src="@drawable/bugcon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Reports"
                    android:textColor="#0E1843"
                    android:textSize="22sp" />

            </LinearLayout>


        </LinearLayout>


    </LinearLayout>

    <ImageView
        android:id="@+id/bgapp"
        android:layout_width="450dp"
        android:layout_height="900dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:scaleType="fitXY"
        android:src="@drawable/bgapp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.309"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ImageView bgapp, clover;
    LinearLayout textsplash, texthome, menus;
    Animation frombottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frombottom = AnimationUtils.loadAnimation(this, R.anim.frombottom);


        bgapp = (ImageView) findViewById(R.id.bgapp);
        clover = (ImageView) findViewById(R.id.clover);
        textsplash = (LinearLayout) findViewById(R.id.textsplash);
        texthome = (LinearLayout) findViewById(R.id.texthome);
        menus = (LinearLayout) findViewById(R.id.menus);

        bgapp.animate().translationY(-2400).setDuration(450).setStartDelay(1500);
        clover.animate().alpha(0).setDuration(800).setStartDelay(600);
        textsplash.animate().translationY(140).alpha(0).setDuration(800).setStartDelay(300);

        texthome.startAnimation(frombottom);
        menus.startAnimation(frombottom);


    }
}

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

正如我所说,我认为图像视图尺寸是问题。因为它取决于dpi。因此您必须像动画一样设置百分比大小。您可以将constraintlayout用作父对象,并使用百分比设置宽度和高度

像这样:

<ImageView
        android:layout_width="0dp"
        app:layout_constraintWidth_percent=".5"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent=".2"/>

答案 1 :(得分:0)

我解决了问题

对我来说,translationY的理想值是440dpi为-1900,所以我做了1 + 00/440 =4,318181818181818‬,所以我可以使用4,318181818181818‬进行1dpi

float h = 4.318181818181818f;
DisplayMetrics dm = this.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
float tr=densityDpi*h;
bgapp.animate().translationY(-tr).setDuration(450).setStartDelay(1500);

我认为这不是完美的解决方案,但目前可以使用