簡體   English   中英

翻譯動畫以在Android中正確定位

[英]Translate animation to correct position in Android

我已經在應用程序中為圖像創建了動畫。 圖像從屏幕的中間開始到左上角。 現在,我需要確保將圖像放置在左上角所有設備中的正確位置。 當前,它被放置在左上角不同設備的不同位置。 我該如何解決? 我的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="-36%p"
        android:fromYDelta="0%p"
        android:toYDelta="-30.9%p"
        android:duration="500"
        android:fillAfter="true" />
</set>

誰能幫忙。

我在此方面取得了一些不錯的結果,因此希望對您或任何一個人有幫助。 這個想法很簡單。

通過xml將要動畫的內容定位為其最終位置,然后使用TranslateAnimation並將其從值設置為任何float值並將值設置0 這樣,小部件/視圖會設置動畫,並停留在您將其放置在xml中的位置。

一個小示例,用於將屏幕外的TextView動畫TextView xml中指定的位置。

//This is just for getting the from value, from where animation starts.
WindowManager wm = (WindowManager) mContext
        .getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();

Animation  moveRighttoLeft = new TranslateAnimation(width, 0, 0, 0);
moveRighttoLeft.setDuration(700);

AnimationSet animation = new AnimationSet(false);
animation.addAnimation(moveRighttoLeft);
myTextView.setAnimation(animation);

這是xml的

<TextView
    android:id="@+id/mytextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is a Text View"
    android:layout_marginLeft="30dp"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM