如何使imageview成曲线

时间:2013-04-02 01:50:21

标签: android android-layout android-intent android-widget android-animation

我是Android的新手...我想制作一个摩天轮游戏,我需要移动轮子中的摩天轮椅...我使用:

          TranslateAnimation transAnimation1 = new TranslateAnimation(0, 145, 0, 275);
          transAnimation1.setDuration(6000);
          ChairA1.startAnimation(transAnimation1);

          TranslateAnimation transAnimation2 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation2.setDuration(6000);
          ChairA2.startAnimation(transAnimation2);

          TranslateAnimation transAnimation3 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation3.setDuration(6000);
          ChairA3.startAnimation(transAnimation3);

          TranslateAnimation transAnimation4 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation4.setDuration(6000);
          ChairA4.startAnimation(transAnimation4);

          TranslateAnimation transAnimation5 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation5.setDuration(6000);
          ChairA5.startAnimation(transAnimation5);

          TranslateAnimation transAnimation6 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation6.setDuration(6000);
          ChairA6.startAnimation(transAnimation6);

          TranslateAnimation transAnimation7 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation7.setDuration(6000);
          ChairA7.startAnimation(transAnimation7);

          TranslateAnimation transAnimation8 = new TranslateAnimation(0, 50, 0, 50);
          transAnimation8.setDuration(6000);
          ChairA8.startAnimation(transAnimation8);

我的摩天轮上有8把椅子,但问题是轮子是圆形的,我不能让ImageView(椅子)在曲线目的地......有人会帮助我......我真的需要一个帮助吗? / p>

1 个答案:

答案 0 :(得分:0)

这是代码。它编写得不好,但它能完成这项工作。不要问我如何做椅子的初始定位。我只是使用了RelativeLayout并猜到了边缘。为了使这个动画看起来很好并且在不同的手机上正确对齐,你仍然需要做很多工作。

以下是MainActivity.java

package com.example.ferriswheel;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
    RelativeLayout ferrisWheelLayout;
    ObjectAnimator spin;
    ImageView chairView1, chairView2, chairView3, chairView4;
    ObjectAnimator counterSpin1, counterSpin2, counterSpin3, counterSpin4;
    boolean currentlySpinning;

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

    public void onToggleClicked(View view) {
        if (currentlySpinning) {
            stopAnimation(view); 
            currentlySpinning = false;
        } else {
            startAnimation(view);
            currentlySpinning = true;
        }
    }


    public void stopAnimation(View view) {
        spin.cancel();
        counterSpin1.cancel();
        counterSpin2.cancel();
        counterSpin3.cancel();
        counterSpin4.cancel();  
    }

    public void startAnimation(View view) {
        chairView1 = (ImageView) findViewById(R.id.chair1);
        chairView2 = (ImageView) findViewById(R.id.chair2);
        chairView3 = (ImageView) findViewById(R.id.chair3);
        chairView4 = (ImageView) findViewById(R.id.chair4);
        ferrisWheelLayout = (RelativeLayout) findViewById(R.id.parentWheelLayout);

        spin = ObjectAnimator.ofFloat(ferrisWheelLayout, "rotation", 0f, 360f);
        spin.setRepeatMode(-1);
        spin.setRepeatCount(Animation.INFINITE);
        spin.setDuration(8000);
        spin.setInterpolator(new LinearInterpolator());
        spin.start();

        counterSpin1 = ObjectAnimator
                .ofFloat(chairView1, "rotation", 0f, -360f);
        counterSpin1.setRepeatMode(-1);
        counterSpin1.setRepeatCount(Animation.INFINITE);
        counterSpin1.setDuration(8000);
        counterSpin1.setInterpolator(new LinearInterpolator());
        counterSpin1.start();

        counterSpin2 = ObjectAnimator
                .ofFloat(chairView2, "rotation", 0f, -360f);
        counterSpin2.setRepeatMode(-1);
        counterSpin2.setRepeatCount(Animation.INFINITE);
        counterSpin2.setDuration(8000);
        counterSpin2.setInterpolator(new LinearInterpolator());
        counterSpin2.start();

        counterSpin3 = ObjectAnimator
                .ofFloat(chairView3, "rotation", 0f, -360f);
        counterSpin3.setRepeatMode(-1);
        counterSpin3.setRepeatCount(Animation.INFINITE);
        counterSpin3.setDuration(8000);
        counterSpin3.setInterpolator(new LinearInterpolator());
        counterSpin3.start();

        counterSpin4 = ObjectAnimator
                .ofFloat(chairView4, "rotation", 0f, -360f);
        counterSpin4.setRepeatMode(-1);
        counterSpin4.setRepeatCount(Animation.INFINITE);
        counterSpin4.setDuration(8000);
        counterSpin4.setInterpolator(new LinearInterpolator());
        counterSpin4.start();

    }
}

这是它的布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentWheelLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:onClick="onToggleClicked" >

    <ImageView
        android:id="@+id/wheelSpokes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/chair1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="100dp"
        android:src="@drawable/ic_launcher" />

        <ImageView
        android:id="@+id/chair2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="100dp"
        android:src="@drawable/ic_launcher" />

        <ImageView
        android:id="@+id/chair3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="300dp"
        android:src="@drawable/ic_launcher" />

         <ImageView
             android:id="@+id/chair4"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="top"
             android:layout_marginBottom="100dp"
             android:layout_marginLeft="200dp"
             android:layout_marginRight="100dp"
             android:layout_marginTop="300dp"
             android:adjustViewBounds="true"
             android:src="@drawable/ic_launcher" />


</RelativeLayout>

代码背后的一些理论:

想象一下摩天轮单向旋转90度。在那段时间里,摩天轮上的一把椅子通常做什么?那把椅子沿着车轮的轴线与摩天轮一起移动,同时,椅子也在自己的轴上旋转了零下90度。

在换句话说,我们可以有椅子开始其自身的轴线旋转,并在同一时间,如果我们可以通过在较大视图包围它结合了摩天轮和椅子,那么我们就可以旋转,较大从较大的轴以相反的方向以相反的方向查看。

希望这对你有意义。