Android Tween动画:无法在同一文件中跨不同的线性布局移动TextView

时间:2015-03-12 09:34:44

标签: android android-layout android-animation tween

我似乎找不到在同一文件中跨不同线性布局移动TextView的方法。当我点击TextView时,它会消失,直到它达到它自己的布局。

有人可以帮忙吗?文件已附加。

这是运行的mainactivity.java文件。

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    Animation animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FillBlanks question = generate_question();
        setContentView(R.layout.fill_up_template);
        setUpUI(question);
    }

    private void setUpUI(FillBlanks question) {
        // TODO Auto-generated method stub
        TextView category = (TextView) findViewById(R.id.fillBlank_category);
        category.setText(question.get_category());
        TextView questionPart1 = (TextView) findViewById(R.id.fillBlank_qp1);
        questionPart1.setText(question.get_questionPart1());
        TextView questionPart2 = (TextView) findViewById(R.id.fillBlank_qp2);
        questionPart2.setText(question.get_questionPart2());
        TextView option1 = (TextView) findViewById(R.id.fillBlank_option1);
        option1.setText(question.get_option1());
        TextView option2 = (TextView) findViewById(R.id.fillBlank_option2);
        option2.setText(question.get_option2());
        TextView option3 = (TextView) findViewById(R.id.fillBlank_option3);
        option3.setText(question.get_option3());
        TextView option4 = (TextView) findViewById(R.id.fillBlank_option4);
        option4.setText(question.get_option4());
        questionPart1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                getInit(v);
            }
        });


    }

    private FillBlanks generate_question() {
        FillBlanks question = new FillBlanks("We", "chocolates", "is", "am", "have", "has","English","have");
        return question;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void translateToAnswer(View V){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        Log.d("App","OnCLik");
        TextView t = (TextView) V;
        //t.startAnimation(animation);
        System.out.print("damn");
        int[] blank_coordinates = new int[2];
        int[] option_coordinates = new int[2];
        TextView blank = (TextView) findViewById(R.id.fillBlank_blank);
        blank.getLocationOnScreen(blank_coordinates);
        V.getLocationOnScreen(option_coordinates);
        Log.d("coordinates","damn"+String.valueOf(option_coordinates[0])+" "+ String.valueOf(blank_coordinates[0])+" "+ String.valueOf(option_coordinates[1])+" " + String.valueOf(blank_coordinates[1]));
        animation = new TranslateAnimation(option_coordinates[0], blank_coordinates[0], option_coordinates[1], blank_coordinates[1]);
        animation.setDuration(4000);
        animation.setRepeatCount(0);
        animation.setStartOffset(1000);
        animation.setFillEnabled(true);
        animation.setFillAfter(true);
        V.startAnimation(animation);
        blank.getLocationOnScreen(blank_coordinates);
        V.getLocationOnScreen(option_coordinates);
        Log.d("coordinates","damn"+String.valueOf(option_coordinates[0])+" "+ String.valueOf(blank_coordinates[0])+" "+ String.valueOf(option_coordinates[1])+" " + String.valueOf(blank_coordinates[1]));

    }

    public void getInit(View V) {

        //translatetext = (TextView) findViewById(R.id.translatetext);
        animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        //translatetext.startAnimation(animation);
        TextView t = (TextView) V;
        t.startAnimation(animation);

    }
}

这是布局文件:

<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"
    android:paddingBottom="10dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="10dp"
    tools:context="com.example.gsocdemo.MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp" >

        <TextView
            android:id="@+id/fillBlank_qp1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="20dp"
            android:text="qp1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/fillBlank_blank"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#DDDDDD"
            android:paddingLeft="18dp"
            android:paddingRight="18dp"
            android:text="      "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/fillBlank_qp2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text=" qp2"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="104dp"
        android:padding="5dp" >

        <TextView
            android:id="@+id/fillBlank_option1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="op1"
            android:onClick="translateToAnswer"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/fillBlank_option2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="op2"
            android:onClick="translateToAnswer"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/fillBlank_option3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="op3"
            android:onClick="translateToAnswer"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/fillBlank_option4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="op4"
            android:onClick="translateToAnswer"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <TextView
        android:id="@+id/fillBlank_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Category"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/fillBlank_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="32dp"
        android:ellipsize="start"
        android:padding="10dp"
        android:text="Check" />

</RelativeLayout>

这是动画:

 <?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXDelta="200" 
    android:toXDelta="0" 
    android:fromYDelta="200" 
    android:toYDelta="0" 
    android:duration="4000" 
    android:startOffset="1000" 
    android:repeatCount="0" 
    android:repeatMode="restart" 
    android:fillEnabled="false" 
    android:fillBefore="false" 
    android:fillAfter="false" >
</translate>

0 个答案:

没有答案