我正在构建我的第一个游戏(Rummy游戏)。我无法为扑克牌分发找到一个好的动画效果。这是我认为动画应该有的步骤:
我写的代码是:
MainActivity.java
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{
ImageView iv1,iv2,iv3,iv4,iv5,iv6,iv7,iv8;
Animation animFadein,animMove;
int height,width;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
iv2=(ImageView)findViewById(R.id.iv2);
iv1=(ImageView)findViewById(R.id.iv1);
iv3=(ImageView)findViewById(R.id.iv3);
iv4=(ImageView)findViewById(R.id.iv4);
iv5=(ImageView)findViewById(R.id.iv5);
iv6=(ImageView)findViewById(R.id.iv6);
iv7=(ImageView)findViewById(R.id.iv7);
iv8=(ImageView)findViewById(R.id.iv8);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final TranslateAnimation animationt = new TranslateAnimation(0, convertDpToPixel(-85,this), 0, convertDpToPixel(80,this));
animationt.setDuration(300);
animationt.setFillAfter(true);
animationt.setAnimationListener(this);
final TranslateAnimation animationt2 = new TranslateAnimation(0, convertDpToPixel(-65,this), 0, convertDpToPixel(80,this));
animationt2.setDuration(300);
animationt2.setFillAfter(true);
animationt2.setAnimationListener(this);
final TranslateAnimation animationt3 = new TranslateAnimation(0, convertDpToPixel(-45,this), 0, convertDpToPixel(80,this));
animationt3.setDuration(400);
animationt3.setFillAfter(true);
animationt3.setAnimationListener(this);
final TranslateAnimation animationt4 = new TranslateAnimation(0, convertDpToPixel(-25,this), 0, convertDpToPixel(80,this));
animationt4.setDuration(500);
animationt4.setFillAfter(true);
animationt4.setAnimationListener(this);
final TranslateAnimation animationt5 = new TranslateAnimation(0, convertDpToPixel(5,this), 0, convertDpToPixel(80,this));
animationt5.setDuration(600);
animationt5.setFillAfter(true);
animationt5.setAnimationListener(this);
final TranslateAnimation animationt6 = new TranslateAnimation(0, convertDpToPixel(25,this), 0, convertDpToPixel(80,this));
animationt6.setDuration(700);
animationt6.setFillAfter(true);
animationt6.setAnimationListener(this);
final TranslateAnimation animationt7 = new TranslateAnimation(0, convertDpToPixel(45,this), 0, convertDpToPixel(80,this));
animationt7.setDuration(800);
animationt7.setFillAfter(true);
animationt7.setAnimationListener(this);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
iv1.startAnimation(animFadein);
iv8.startAnimation(animationt);
iv7.startAnimation(animationt2);
iv6.startAnimation(animationt3);
iv5.startAnimation(animationt4);
iv4.startAnimation(animationt5);
iv3.startAnimation(animationt6);
iv2.startAnimation(animationt7);
}
});
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.sequential);
animMove=AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
// set animation listener
animFadein.setAnimationListener(this);
// button click event
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
iv1.clearAnimation();
iv1.setRotation(0f);
// iv2.startAnimation(animMove);
// iv2.startAnimation(animationt);
}
/* if(animation==animMove)
{
iv2.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(iv2.getWidth(), iv2.getHeight());
lp.setMargins(con, 300, 0, 0);
iv2.setLayoutParams(lp);
}
*/
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return dp;
}
}
动画文件夹中的sequential.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<!-- Use startOffset to give delay between animations -->
<rotate
android:duration="300"
android:fromDegrees="0"
android:interpolator="@android:anim/cycle_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:repeatCount="0"
android:repeatMode="restart"
android:toDegrees="70" />
<!-- Move -->
</set>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.rushiayappa.animationsample.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv8"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv7"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv6"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv5"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv4"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv3"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv2"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv1"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
/>
我这样做几乎就像我想要的那样。但是调出卡时的形状应该像蛇一样..(曲折)