Android 養成記-1 --1.3 標籤拾色器
阿新 • • 發佈:2018-12-04
直接上圖:
建立seekBarDialog ,並將拾色器整合到dialog 中:
public class MySeekBarDialog extends AlertDialog implements TextWatcher { private SeekBar seekBar; private EditText tView; private TextView titleTxv; //標題 private TextView msgTxv; //提示內容文字 private TextView positiveTxv; //確定按鈕 private TextView negativeTxv; //取消按鈕 public static int mColor ; public static String mtagName; /* �Զ��幹�캯�����ڳ�ʼ�� */ public MySeekBarDialog(Context context) { super(context,R.style.custom_dialog); final View view = getLayoutInflater().inflate(R.layout.dialog_seekbar, null); seekBar = (SeekBar) view.findViewById(R.id.SeekBar01); tView = (EditText)view.findViewById(R.id.textview); titleTxv = (TextView) view.findViewById(R.id.title); msgTxv = (TextView) view.findViewById(R.id.message); positiveTxv = (TextView) view.findViewById(R.id.positiveButton); negativeTxv = (TextView) view.findViewById(R.id.negativeButton); tView.addTextChangedListener(textWatcher);//設定文字編輯的實時監聽 setView(view); initColorBar(); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar arg0) { } @Override public void onStartTrackingTouch(SeekBar arg0) { } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ColorPickGradient mColorPickGradient = new ColorPickGradient(); float radio = (float)progress / seekBar.getMax(); //獲取當前滑動位置 mColor = mColorPickGradient.getColor(radio);//採集當前位置的顏色 seekBar.getThumb().setColorFilter(mColor, Mode.SRC_ATOP); //設定thumb的顏色 //seekBar.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_ATOP); // mtagName = tView.getText().toString(); // change.change(progress); // tView.setText("���ȣ�" + progress + "%"); } }); } private void initColorBar() { // TODO Auto-generated method stub //設定SeekBar的顏色 ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { LinearGradient linearGradient = new LinearGradient(0, 0, width, height, ColorPickGradient.PICKCOLORBAR_COLORS, ColorPickGradient.PICKCOLORBAR_POSITIONS, Shader.TileMode.REPEAT); return linearGradient; } }; PaintDrawable paint = new PaintDrawable(); paint.setShape(new RectShape()); paint.setCornerRadius(10); paint.setShaderFactory(shaderFactory); seekBar.setProgressDrawable(paint); } @Override public void setTitle(CharSequence title) { titleTxv.setText(title); } @Override public void setTitle(int titleId) { setTitle(getContext().getString(titleId)); } /** * 設定提示內容文字 * @param msg */ public void setMessage(CharSequence msg){ msgTxv.setText(msg); } /** * 設定確定鍵文字 * @param text */ public void setPositiveText(CharSequence text){ positiveTxv.setText(text); } /** * 設定取消鍵文字 * @param text */ public void setNegativeText(CharSequence text){ negativeTxv.setText(text); } /** * 取消鍵監聽器 * @param onClickListener */ public void setNegativeButton(CharSequence text,View.OnClickListener onClickListener){ negativeTxv.setText(text); negativeTxv.setOnClickListener(onClickListener); } /** * 確定鍵監聽器 * @param */ public void setPositiveButton(String text, View.OnClickListener onClickListener) { // TODO Auto-generated method stub positiveTxv.setText(text); positiveTxv.setOnClickListener(onClickListener); } public void setEditContent(String string) { // TODO Auto-generated method stub if (string ==null) return; tView.setText(string); } public void setEdit(EditText et) { // TODO Auto-generated method stub } @Override public void onPointerCaptureChanged(boolean hasCapture) { // TODO Auto-generated method stub } @Override public void onProvideKeyboardShortcuts( List<KeyboardShortcutGroup> data, Menu menu, int deviceId) { // TODO Auto-generated method stub } private TextWatcher textWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // System.out.println("-1-onTextChanged-->" + tView.getText().toString() + "<--"); } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub mtagName = tView.getText().toString(); Log.v("mtagName", mtagName); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } }; @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub }
拾色器的封裝介面:
public class ColorPickGradient { //設定的顏色 public static final int[] PICKCOLORBAR_COLORS = new int[]{0xFFFFFFFF,0xFFFF3030, 0xFFF4A460, 0xFFFFFF00, 0xFF66CD00, 0xFF458B00, 0xFF0000EE, 0xFF912CEE,0xFF000000}; //每個顏色的位置 public static final float[] PICKCOLORBAR_POSITIONS = new float[]{0f, 0.1f, 0.2f, 0.3f, 0.5f, 0.65f,0.8f,0.9f,1f}; private int[] mColorArr = PICKCOLORBAR_COLORS; private float[] mColorPosition = PICKCOLORBAR_POSITIONS; public ColorPickGradient(int[] colorArr) { this.mColorArr = colorArr; } public ColorPickGradient() { } /** * 獲取某個百分比位置的顏色 * @param radio 取值[0,1] * @return */ public int getColor(float radio) { int startColor; int endColor; if (radio >= 1) { return mColorArr[mColorArr.length - 1]; } for (int i = 0; i < mColorPosition.length; i++) { if (radio <= mColorPosition[i]) { if (i == 0) { return mColorArr[0]; } startColor = mColorArr[i - 1]; endColor = mColorArr[i]; float areaRadio = getAreaRadio(radio,mColorPosition[i-1],mColorPosition[i]); return getColorFrom(startColor,endColor,areaRadio); } } return -1; } public float getAreaRadio(float radio, float startPosition, float endPosition) { return (radio - startPosition) / (endPosition - startPosition); } /** * 取兩個顏色間的漸變區間 中的某一點的顏色 * @param startColor * @param endColor * @param radio * @return */ public int getColorFrom(int startColor, int endColor, float radio) { int redStart = Color.red(startColor); int blueStart = Color.blue(startColor); int greenStart = Color.green(startColor); int redEnd = Color.red(endColor); int blueEnd = Color.blue(endColor); int greenEnd = Color.green(endColor); int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5)); int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5)); int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5)); return Color.argb(255, red, greed, blue); }
主要是知道怎麼使用就行了。
本demo根據網路資源修改,侵刪!!
demo 在如下的連結中,可以直接執行: