public class ArrowView extends View{
private Context mContext = null;
@Override
protected void onDraw(Canvas canvas) {
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth()/2, getHeight());
path.lineTo(0, 0);
ShapeDrawable circle = new ShapeDrawable(new PathShape(path, getWidth(), getHeight()));
circle.getPaint().setColor(mContext.getResources().getColor(R.color.download_bg_menu));
circle.setBounds(0, 0, getWidth(), getHeight());
circle.draw(canvas);
super.onDraw(canvas);
}
public ArrowView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
}
如果三角的底色设置为背景色一致,以下是是方便区分。
原理是开启一个PopWindow,还可以为开启PopWindow设置一个启动动画效果。
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRootView = mInflater.inflate(R.layout.task_menu_view, null);
mWindow = new PopupWindow(mContext);
mWindow.setAnimationStyle(R.style.anim_menu_window);
//触碰其他的地方就关闭
mWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
XLDownloadMenuWindow.this.mWindow.dismiss();
return true;
}
return false;
}
});
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="anim_menu_window"> <item name="android:windowEnterAnimation">@anim/anim_menu_show</item> <item name="android:windowExitAnimation">@anim/anim_menu_dismiss</item> </style> </resources>
show popwindow
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="100" android:fromXScale="1.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="100%" android:pivotY="100%" android:toXScale="1.0" android:toYScale="1.0" /> </set>
dismiss popwindow
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="100" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="100%" android:pivotY="100%" android:toXScale="1.0" android:toYScale="0.0" /> </set>
package com.xunlei.downloadplatforms.sample3;
import com.xunlei.downloadplatforms.sample3.XLDownloadClientConstants.MenuOperateType;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class XLDownloadMenuWindow {
private Context mContext = null;
private LayoutInflater mInflater = null;
private View mRootView = null;
private PopupWindow mWindow = null;
private Drawable mBGDrawable = null;
private Handler mMenuOperateHandler = null;
private Button mBtnInit = null;
private Button mBtnUninit = null;
private Button mBtnExit = null;
private int mRootWidth;
private int mRootHeight;
public XLDownloadMenuWindow(Context context, Handler handler) {
mContext = context;
mMenuOperateHandler = handler;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRootView = mInflater.inflate(R.layout.task_menu_view, null);
mWindow = new PopupWindow(mContext);
//设置动画文件
mWindow.setAnimationStyle(R.style.anim_menu_window);
//触碰其他的地方就关闭
mWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
XLDownloadMenuWindow.this.mWindow.dismiss();
return true;
}
return false;
}
});
initControllers();
}
private void initControllers() {
mBtnInit = (Button) mRootView.findViewById(R.id.task_menu_btn_init);
mBtnUninit = (Button) mRootView.findViewById(R.id.task_menu_btn_uninit);
mBtnExit = (Button) mRootView.findViewById(R.id.task_menu_btn_exit);
mBtnInit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mMenuOperateHandler instanceof Handler) {
Message msg = mMenuOperateHandler.obtainMessage();
msg.what = MenuOperateType.INIT;
msg.sendToTarget();
}
mWindow.dismiss();
}
});
mBtnUninit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mMenuOperateHandler instanceof Handler) {
Message msg = mMenuOperateHandler.obtainMessage();
msg.what = MenuOperateType.UNINIT;
msg.sendToTarget();
}
mWindow.dismiss();
}
});
mBtnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mMenuOperateHandler instanceof Handler) {
Message msg = mMenuOperateHandler.obtainMessage();
msg.what = MenuOperateType.EXIT;
msg.sendToTarget();
}
}
});
}
public void show(View authorView) {
if(authorView instanceof View && mWindow instanceof PopupWindow) {
if(!mWindow.isShowing()) {
int[] location = new int[2];
authorView.getLocationOnScreen(location);
Rect authorRect = new Rect(location[0], location[1], location[0] + authorView.getWidth(), location[1]
+ authorView.getHeight());
preShow();
mRootView.measure(0, 0);
mRootWidth = mRootView.getMeasuredWidth();
mRootHeight = mRootView.getMeasuredHeight();
int xPos = 0;
int yPos = authorRect.top - mRootHeight;
mWindow.showAtLocation(authorView, Gravity.NO_GRAVITY, xPos, yPos);
} else {
mWindow.dismiss();
}
}
}
private void preShow() {
if (mBGDrawable == null) {
mWindow.setBackgroundDrawable(new BitmapDrawable());
} else {
mWindow.setBackgroundDrawable(mBGDrawable);
}
mWindow.setWidth(MarginLayoutParams.MATCH_PARENT);
mWindow.setHeight(MarginLayoutParams.WRAP_CONTENT);
mWindow.setTouchable(true);
mWindow.setOutsideTouchable(true);
mWindow.setFocusable(true);
mWindow.setContentView(mRootView);
}
}