简体   繁体   English

如何设置对象动画制作器以跨各种屏幕尺寸制作动画

[英]How to set object animator to animate across various screen sizes

So I have this animation I have been working on. 所以我有这个动画我一直在努力。 Essentially, when you click the image all it does is simply animate the image to a new Y coordinate. 本质上,单击图像时,它所做的只是将图像动画化为新的Y坐标。 Note: Keep in mind that my images are all scaled appropriately for mdli, hdpi, xhdpi etc. So I have 2 problems: 1 . 注意:请记住,我的图像都针对mdli,hdpi,xhdpi等进行了适当缩放。 因此,我有2个问题: 1 When I use .setY on an image view, the y coordinate is loaded differently on other phones. 当我在图像视图上使用.setY时,y坐标在其他手机上的加载方式有所不同。 2 . 2 When I use both my image and txt animate methods, they both animate to different y coordinates based on different phones/screens. 当我同时使用图像和txt动画方法时,它们都将根据不同的手机/屏幕动画到不同的y坐标。 So my question is how do I fix both of these problems so that they are consistent across all screen sizes? 因此,我的问题是如何解决这两个问题,以便在所有屏幕尺寸上都保持一致?

public class MainActivity extends Activity {

    final String tag = "My activity";

    ImageView mWhatsTheAnswerImg, mInspireMeImg, mHelpMeThink, mTalkToVince;


    ObjectAnimator mImageAnimation, mTxtAnimation;



    TextView mWhatsTheAnswerText, mInspireMeTxt, mHelpMeThinkTxt, mTalkToVinceTxt;
    boolean mWhatsTheAnswerBOOL,  mInspireMeBOOL, mHelpMeThinkBOOL, mTalkToVinceBOOL =false;


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



        ActionBar actionBar = getActionBar();
        actionBar.hide();



        mInspireMeImg = (ImageView) findViewById(R.id.inspireme);

        mInspireMeImg.setY(750);

        mInspireMeTxt = (TextView) findViewById(R.id.inspireMeTxt);

        mInspireMeTxt.setY(750);


        mInspireMeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mInspireMeBOOL) {



                    imageAnimate(mInspireMeImg, 750);
                    txtAnimate(mInspireMeTxt, 750);

                    Log.w(tag, "DOWN Coordinates: " + mInspireMeImg.getY() + "Txt: " + mInspireMeTxt.getY());





                    mInspireMeBOOL = false;
                } else {



                    mInspireMeBOOL = true;



                    imageAnimate(mInspireMeImg, +290);
                    txtAnimate(mInspireMeTxt, +290);

                    Log.w(tag, "UP Coordinates: " + mInspireMeImg.getY() + "Txt: " + mInspireMeTxt.getY());



                }

            }
        });

Image animate and txt animate methods: 图像动画和txt动画方法:

private void imageAnimate(ImageView img, float YCoordinates){

     mImageAnimation =ObjectAnimator.ofFloat(img, View.TRANSLATION_Y,YCoordinates);

        Log.w(tag, "IMG Coordinates: " + img.getY());

        mImageAnimation.setDuration(1000);

        mImageAnimation.start();

    }


    private void txtAnimate(TextView txt, float YCoordinates){


        mTxtAnimation = ObjectAnimator.ofFloat(txt, View.TRANSLATION_Y, YCoordinates);

        Log.w(tag, "TXT Coordinates: " + txt.getY());


        mTxtAnimation.setDuration(1000);

        mTxtAnimation.start();
    }



}

Edit: XML 编辑:XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.wyatt.avinceandroid.app.MainActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Background"
        android:src="@drawable/coachvincebackground"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:id="@+id/inspireme"
        android:src="@drawable/inspiremescreen"
        android:scaleType="fitXY"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/inspireMeTxt"
        android:layout_alignTop="@+id/helpMeThink"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="89dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:id="@+id/helpMeThink"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:src="@drawable/helpmethinkscreen"
        android:scaleType="fitXY"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/helpMeThinkTxt"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:id="@+id/whatsTheAnswer"
        android:src="@drawable/whatstheanswerscreen"
        android:scaleType="fitXY"
      />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/whatsTheAnswerTxt"
        android:layout_alignTop="@+id/AskVince"
        android:layout_alignLeft="@+id/inspireMeTxt"
        android:layout_alignStart="@+id/inspireMeTxt" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:id="@+id/AskVince"
        android:src="@drawable/askvincescreen"
        android:scaleType="fitXY"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/askVinceTxt"
        android:layout_below="@+id/helpMeThinkTxt"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

The setY and other similar methods expect values in pixels. setY和其他类似方法期望以像素为单位的值。 Now, this is exactly where the problem lies. 现在,这正是问题所在。 Different phones have screens of different resolutions and so the 750th pixel will be located at different positions on different phones unless two displays are identical. 不同的手机具有不同分辨率的屏幕,因此除非两个显示器相同,否则第750个像素将位于不同手机上的不同位置。

The solution is simple. 解决方案很简单。 Use values in DP : DP使用值:

mInspireMeImg.setY(dpToPx(750));

and keep this method stored somewhere: 并将此方法存储在某个位置:

public static int dpToPx(float dpValue, Context context){
    return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
}

More about DP unit: 有关DP单元的更多信息:

http://developer.android.com/training/multiscreen/screendensities.html http://developer.android.com/training/multiscreen/screendensities.html

What is the difference between "px", "dp", "dip" and "sp" on Android? 在Android上,“ px”,“ dp”,“ dip”和“ sp”有什么区别?

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

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