页面翻转Android

时间:2012-10-04 20:26:05

标签: android

我必须在调用视图鳍状肢时创建动画。我一直在寻找一种方法来创建一个与TextView一起使用的动画,但大多数动画都是从drawable文件夹中呈现的图像或转换为图像。

  

有人可以给我一些创建页面翻转动画的提示   textview类似于动画类?

    public static Animation inFromRightAnimation() {

        Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(350);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
        }

将ViewFlipper称为vf

            vf.setInAnimation(AnimationHelper.inFromRightAnimation());
            vf.setOutAnimation(AnimationHelper.outToLeftAnimation());

1 个答案:

答案 0 :(得分:1)

我使用一个单独的java文件来处理像这样的动画

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;

 public class TransparentPanel extends LinearLayout 
{ 
private Paint   innerPaint, borderPaint ;

public TransparentPanel(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public TransparentPanel(Context context) {
    super(context);
    init();
}

private void init() {
    innerPaint = new Paint();
    innerPaint.setARGB(225, 75, 75, 75); //gray
    innerPaint.setAntiAlias(true);

    borderPaint = new Paint();
    borderPaint.setARGB(255, 255, 255, 255);
    borderPaint.setAntiAlias(true);
    borderPaint.setStyle(Style.STROKE);
    borderPaint.setStrokeWidth(2);
}

public void setInnerPaint(Paint innerPaint) {
    this.innerPaint = innerPaint;
}

public void setBorderPaint(Paint borderPaint) {
    this.borderPaint = borderPaint;
}

@Override
protected void dispatchDraw(Canvas canvas) {

    RectF drawRect = new RectF();
    drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());

    canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
    canvas.drawRoundRect(drawRect, 5, 5, borderPaint);

    super.dispatchDraw(canvas);
}
}

然后在你的res文件夹中添加另一个名为anim的文件并包含此文件

popup_hide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="750"/>
</set>

popup_show.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="750"/>
</set>

完成此操作后,将其添加到main.xml或将要使用动画的那个:

<com.your.app.name.TransparentPanel
        android:id="@+id/popup_view" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="left"
        android:background="@android:color/transparent">
        <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/popup_textview""/>
        <TextView android:id="@+id/textView" 
        android:layout_width="wrap_content" 
        android:text="Periodicos:" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceMedium"> </TextView>
        <Button android:id="@+id/hide_text_button"
                style="?android:attr/buttonStyleSmall"
                android:textStyle="bold"
                android:text="Close" 
                android:background="@drawable/button"/>
                </LinearLayout>
</com.your.app.name.TransparentPanel>

并在您的主Activity.java中使用以下方法调用动画:

在onCreate之前

private Animation animShow, animHide;
在onCreate之后

final TransparentPanel g = (TransparentPanel) findViewById(R.id.popup_view);    
animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);

g.startAnimation( animHide ); //to hide
g.setVisibility(View.GONE);

g.startAnimation( animShow );// to show
g.setVisibility(View.VISIBLE);

很长但它有效,享受。