我使用一个单独的 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);
很长,但它有效,享受。