我通过参考以下链接制作动画效果。 http://www.edumobile.org/android/android-development/custom-popup-window-example/comment-page-1/#comment-1649。
代码 :
public class TutorialActivity extends Activity implements OnClickListener {
private static String TAG = TutorialActivity.class.getSimpleName();
private static final String NULL_STRING = "null";
private Button mInfoBtn;
private Animation animShow, animHide;
private AlertDialog mErrorAlertDialog;
private boolean isLogOff = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tutorial);
mInfoBtn = (Button) findViewById(R.id.btn_info);
mInfoBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
initPopup();
}
/**
* Used to show the Logoff Popup menu
*/
private void initPopup() {
Intent intent = getIntent();
final PopupPanel popup = (PopupPanel) findViewById(R.id.popup_window);
popup.setVisibility(View.VISIBLE);
animShow = AnimationUtils.loadAnimation(this, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation(this, R.anim.popup_hide);
popup.startAnimation(animShow);
final Button closeButton = (Button) findViewById(R.id.btnClose);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation(animHide);
popup.setVisibility(View.GONE);
}
});
final Button cancelAppointmet = (Button) findViewById(R.id.btnCancel);
final Button logoff = (Button) findViewById(R.id.btnLogoff);
logoff.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation(animHide);
popup.setVisibility(View.GONE);
}
});
cancelAppointmet.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation(animHide);
popup.setVisibility(View.GONE);
}
});
}
}
弹出面板.java
public class PopupPanel extends LinearLayout {
private Paint innerPaint, borderPaint;
public PopupPanel(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PopupPanel(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 0, 0, 0);
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 0, 0, 0);
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);
}
}
教程.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/header_bg_short" >
<Button
android:id="@+id/btn_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10px"
android:background="@drawable/btn_info" >
</Button>
<ImageView
android:id="@+id/iveMailMinder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/header_text" />
</RelativeLayout>
<ImageView
android:id="@+id/tutorilaimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/heading"
android:scaleType="fitXY"
android:src="@drawable/tutorial_android3" />
</RelativeLayout>
<com.xxx.com.PopupPanel
android:id="@+id/popup_window"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="left"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="@+id/btnClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:enabled="false"
android:text="@string/btn_close_name"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnLogoff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:enabled="false"
android:text="@string/btn_logoff_name"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="@string/btn_cancel_name"
android:textSize="16sp"
android:textStyle="bold" />
</com.xxx.com.PopupPanel>
</FrameLayout>
.Code 适用于除Samsung Galaxy S3之外的所有设备。
当我单击“信息”按钮时,我正在显示动画。
观察到在 S3 中,当我长按“信息”按钮时,动画就会出现。正常的点击事件动画不会出现。