自定義文本選擇器
PickerView.png
調(diào)用showPopupWindow 傳入有一個View 上下文Context轻庆,文本字符串String[]
<code>
String[] strings=new String[new Random().nextInt(50)+10];
for (int i = 0; i <strings.length; i++) {
strings[i]="元素"+i;
}
pickerViewPop.showPopupWindow(statistics_element, BaseApplication.getInstance().getContext(),strings
).setOnPickerViewListen(new PickerViewPop.OnPickerViewListen() {
@Override
public void OnConfirm(int index, String text) {
statistics_element.setText(text+"");
}
});
</code>
PopupWindow 顯示
<code>
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Scroller;
import android.widget.TextView;
import com.app.framework.R;
public class PickerViewPop {
private PopupWindow popupWindow;
public PickerViewPop showPopupWindow(View view, Context context, String[] str) {
// 一個自定義的布局,作為顯示的內(nèi)容
View contentView = LayoutInflater.from(context).inflate(
R.layout.picker_view, null);
// 設(shè)置按鈕的點擊事件
setContentView(contentView, str);
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("mengdd", "onTouch : ");
return false;
// 這里如果返回true的話,touch事件將被攔截
// 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點擊外部區(qū)域無法dismiss
}
});
// 如果不設(shè)置PopupWindow的背景呼畸,無論是點擊外部區(qū)域還是Back鍵都無法dismiss彈框
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 設(shè)置好參數(shù)之后再show
popupWindow.showAsDropDown(view);
return this;
}
public interface OnPickerViewListen {
void OnConfirm(int index, String text);
}
private OnPickerViewListen onPickerViewListen;
public void setOnPickerViewListen(OnPickerViewListen onPickerViewListen) {
this.onPickerViewListen = onPickerViewListen;
}
public void setContentView(View contentView, final String[] str) {
final PickerView pickerView = (PickerView) contentView.findViewById(R.id.picker_view);
TextView confirm = (TextView) contentView.findViewById(R.id.confirm);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onPickerViewListen!=null){
int index=pickerView.getSelectedTextIndex();
onPickerViewListen.OnConfirm(index,str[index]);
popupWindow.dismiss();
}
}
});
pickerView.setTextString(str);
}
}
</code>
自定義的文本選擇器
<code>
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.app.framework.utils.DipToPx;
import java.math.BigDecimal;
/**
* 滾動選擇器
* 布局設(shè)置的高度必須能被weight 整除里初, 否則會有偏差
*/
public class PickerView extends View {
private String TAG = "PickerView";
/**
* 數(shù)據(jù)
*/
private String[] strings = new String[]{};
/**
* 控件寬度
*/
private int mViewWidth;
/**
* 控件高度
*/
private int mViewHeight;
/**
* 文本畫筆
*/
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
/**
* 中間線畫筆
*/
private Paint mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
/**
* 中間線邊距
*/
private int linePading = 10;
/**
* 繪制文字大小
*/
private int textSezi = 14;
/**
* 中間線的厚度
*/
private float lineWidth = 1;
/**
* 選中的顏色
*/
private int checkedColor = 0xFFFF0000;
/**
* 未選中的顏色
*/
private int notCheckedColor = 0xFF000000;
/**
* 看見的條目數(shù)量,必須奇數(shù)
*/
private int weight = 5;
private int itemHeight;
/**
* 當(dāng)前的位置
*/
private int index;
/**
* 默認(rèn)選中中間位置
*/
private int defaultIndex = -1;
private int checkedIndex = (weight - 1) / 2;
private float mMoveLen;
public PickerView(Context context) {
super(context);
init();
}
/**
* 設(shè)置默認(rèn)選中位置
*/
public void setDefaultIndex(int defaultIndex) {
this.defaultIndex = defaultIndex;
}
public PickerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mViewHeight = getMeasuredHeight();
mViewWidth = getMeasuredWidth();
itemHeight = mViewHeight / weight;
if (defaultIndex == -1) {
/**設(shè)置默認(rèn)中間位置*/
defaultIndex = strings.length / 2;
}
mMoveLen = -itemHeight * (defaultIndex - (weight - 1) / 2);
this.index = defaultIndex;
}
/**
* 設(shè)置整個控件可視條目數(shù)量
*/
public void setWeight(int weight) {
this.weight = weight;
}
/**
* 獲取選中的位置
*/
public int getSelectedTextIndex() {
return index;
}
/**
* 設(shè)置text文本數(shù)組
*/
public void setTextString(String[] strings) {
this.strings = strings;
}
private void setSelectedText(int index) {
this.index = index;
}
/**
* 設(shè)置繪制文字大小
*/
public void setTextSezi(int textSezi) {
this.textSezi = textSezi;
}
/**
* 設(shè)置繪制中間線邊距
*/
public void setLinePading(int linePading) {
this.linePading = linePading;
}
/**
* 設(shè)置中間線的厚度
*/
public void setLineWidth(float lineWidth) {
this.lineWidth = lineWidth;
}
/**
* 設(shè)置選中時的文本顏色
*/
public void setCheckedColor(int checkedColor) {
this.checkedColor = checkedColor;
}
/***設(shè)置未選中時的文本顏色*/
public void setNotCheckedColor(int notCheckedColor) {
this.notCheckedColor = notCheckedColor;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**繪制文字*/
drwaText(canvas);
/**繪制中間線*/
drwaLine(canvas);
}
/**
* 繪制文字
*
* @param canvas
*/
private void drwaText(Canvas canvas) {
for (int i = 0; i < strings.length; i++) {
/**繪制可視范圍文本*/
if (i + weight -1 > index && i - weight+1 < index) {
if (i == index) {
mPaint.setColor(checkedColor);
} else {
mPaint.setColor(notCheckedColor);
}
Paint.FontMetrics metrics = mPaint.getFontMetrics();
float textHeight = metrics.descent; //文字高度
canvas.drawText(strings[i], mViewWidth / 2, itemHeight * (i + 1) - itemHeight / 2 + mMoveLen + textHeight, mPaint);
}
}
}
/**
* 繪制中間線
*/
private void drwaLine(Canvas canvas) {
int pading = DipToPx.dip2px(linePading);
canvas.drawLine(pading, itemHeight * checkedIndex, getRight() - pading, itemHeight * checkedIndex, mLinePaint);
canvas.drawLine(pading, itemHeight * (checkedIndex + 1), getRight() - pading, itemHeight * (checkedIndex + 1), mLinePaint);
}
/**
* 初始化畫筆
*/
private void init() {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextAlign(Paint.Align.CENTER);//繪制文本橫向居中
mPaint.setTextSize(DipToPx.dip2px(textSezi));
mLinePaint.setStyle(Paint.Style.FILL);
mLinePaint.setTextAlign(Paint.Align.CENTER);
mLinePaint.setStrokeWidth(lineWidth);
}
/**
* 更新畫筆參數(shù)
*/
public void updatePaint() {
init();
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
doDown(event);
break;
case MotionEvent.ACTION_MOVE:
doMove(event);
break;
case MotionEvent.ACTION_UP:
doUp(event);
break;
}
return true;
}
private void doUp(MotionEvent event) {
float indexes = mMoveLen / itemHeight;
/**四舍五入 來判定離哪個位置較近*/
BigDecimal dex = new BigDecimal(indexes).setScale(0, BigDecimal.ROUND_HALF_UP);
float newIndex = dex.floatValue();
mMoveLen = newIndex * itemHeight;
int location;
/*限制角標(biāo)0的移動范圍*/
if (newIndex >= checkedIndex + 1) {
mMoveLen = checkedIndex * itemHeight;
}
/*限制最后一個單位的移動 移動范圍*/
if (newIndex <= -(strings.length - checkedIndex - 1)) {
mMoveLen = -((strings.length - checkedIndex - 1) * itemHeight);
}
// Log.i("newIndex",newIndex+"");
/**防止選中角標(biāo)越界*/
if (newIndex >= 0) {
location = (int) (checkedIndex - newIndex);
if (newIndex >= checkedIndex + 1) {
location = 0;
}
} else {
location = (int) (Math.abs(newIndex) + checkedIndex);
if (location > strings.length - 1) {
location = strings.length - 1;
}
}
/**滑動停止的時候設(shè)置當(dāng)前選中的 是第幾個*/
setSelectedText(location);
invalidate();
}
private void doMove(MotionEvent event) {
int location;
mMoveLen += (event.getY() - mLastDownY);
mLastDownY = event.getY();
float indexes = mMoveLen / itemHeight;
/**四舍五入 來判定離哪個位置較近*/
BigDecimal dex = new BigDecimal(indexes).setScale(0, BigDecimal.ROUND_HALF_UP);
float newIndex = dex.floatValue();
/**防止選中角標(biāo)越界*/
if (newIndex >= 0) {
location = (int) (checkedIndex - newIndex);
if (newIndex >= checkedIndex + 1) {
location = 0;
}
} else {
location = (int) (Math.abs(newIndex) + checkedIndex);
if (location > strings.length - 1) {
location = strings.length - 1;
}
}
setSelectedText(location);
invalidate();
}
private float mLastDownY;
private void doDown(MotionEvent event) {
mLastDownY = event.getY();
}
}
</code>
布局文件 picker_view
<code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.framework.widget.pickerView.PickerView
android:background="@color/white"
android:id="@+id/picker_view"
android:layout_width="100dp"
android:layout_height="200dp" />
<TextView
android:textColor="#111111"
android:id="@+id/confirm"
android:gravity="center"
android:text="確定"
android:textSize="14sp"
android:layout_width="100dp"
android:layout_height="30dp" />
</LinearLayout>
</code>