Android 그림 뒤집기 애니메이션 간단한 구현 코드

4591 단어
다음은 여러분께 재미있는 애니메이션을 공유해 드리겠습니다. 여기는 한 장의 그림을 뒤집기에 비교적 적합합니다. 만약에 여러 장의 그림이라면 APIDemo의 예를 참고하면 ArrayAdapter를 추가하든지 아니면 간단하든지 스스로 수정하여 원하는 것을 실현할 수 있습니다.이곳의 코드는 기본적으로 프로젝트를 직접 운행할 수 있다.이메일xml에 ImageView를 추가합니다.
 
  

android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:textSize="50px"
android:layout_x="150px"
android:layout_y="30px"
android:src="@drawable/ro">
>



이것은 설명할 필요가 없겠지, 다 알아볼 수 있으니까.
마지막으로activity류가 하나 더 필요합니다
예:
 
  
public class TestRotate extends Activity implements OnClickListener{
private mageView imageview;
private ViewGroup mContainer;
/**
* , , ,
*private static final int IMAGE = new int[]{
* R.drawable.ro,
* R.drawable.icon
*};
* ,
*
*/
private static final int IMAGE = R.drawable.ro;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview = (ImageView) findViewById(R.id.image);
mContainer = (ViewGroup) findViewById(R.id.container);
/**
*
* , IMAGE[int]
*
*/
imageview.setImageResource(IMAGE);
/**
*
* ImageView OnClickListener
*
*/
imageview.setClickable(true);
imageview.setFocusable(true);
imageview.setOnClickListener(this);
}
private void applyRotation(int position, float start, float end) {
// Find the center of the container
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
final Rotate3d rotation =
new Rotate3d(start, end, centerX, centerY, 310.0f, true);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position));
mContainer.startAnimation(rotation);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**
*
* ,
* ,
* , , ,
* ,
* -1
*
*/
applyRotation(0,0,90);
}
private final class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private DisplayNextView(int position) {
mPosition = position;
}
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews(mPosition));
}
public void onAnimationRepeat(Animation animation) {
}
}
/**
* This class is responsible for swapping the views and start the second
* half of the animation.
*/
private final class SwapViews implements Runnable {
private final int mPosition;
public SwapViews(int position) {
mPosition = position;
}
public void run() {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3d rotation;
if (mPosition > -1) {
imageview.setVisibility(View.VISIBLE);
imageview.requestFocus();
rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);
} else {
imageview.setVisibility(View.GONE);
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
}

좋은 웹페이지 즐겨찾기