通过进度条控制颜色

来源:互联网 发布:查库存软件 编辑:程序博客网 时间:2024/05/08 06:35

今天是考试完的第一天,说是考试,然而简直就是走走形式。嗨,

回来之后网上找了一个ApkBus上找了一个源代码,看了看勉强能明白写的啥,就是通过一个进度条,控制某个颜色变化。

也不知到有什么用  先写下来,看看保留起来吧。



这个代码 不能先看xml文件了,我们先看看SeekBarcolorPicker这类,

这个类继承了 SeeBar 并且实现了SeekBar.OnSeekBarChangeListener这个接口。

继承完后,有一个内部定义的方法SeekBarColorPicker,都重写,方法里面都初始化一下进度条的最大值。

写一个自定义的监听OnColorChangedListe                                                                                                                                                                                                                                        



看看代码把 

一、SeekBarColorPicker.Java

<span style="font-size:14px;">package com.example.he_jingzhou.seekbarcolordemo;import android.content.Context;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.util.AttributeSet;import android.util.Log;import android.widget.SeekBar;/** * Created by He_jingzhou on 2016/1/4. */public class SeekBarColorPicker extends SeekBar implements SeekBar.OnSeekBarChangeListener {    private int color;    private OnColorChangedListener listener;    public interface OnColorChangedListener {        public void onProgressColorChanged(int color);        public void onStartColorChanged(int color);        public void onStopColorChanged(int color);    }    public SeekBarColorPicker(Context context) {        super(context);        init();    }    public SeekBarColorPicker(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public SeekBarColorPicker(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        setMax(256 * 6 - 1);        setOnSeekBarChangeListener(this);//拖动条的监听  包括启动停止    }    /**     * onSizeChanged  这个方法是在 view改变大小是进行调用     *     * @param w    新的宽度     * @param h    新的高度     * @param oldw old宽度     * @param oldh old高度     */    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);    }    public void setOnSeebarColorChangedListener(OnColorChangedListener listener) {        this.listener = listener;    }    /**     * 获取当前的颜色     *     * @return 当前颜色     */    public int getCurrentColor() {        return color;    }    /**     * onProgressChanged 进度条发生变化     *     * @param seekBar  这个滑动条的进步改变了     * @param progress 当前的进度水平  从0开始的 最大 call setMax(int x).默认100     * @param fromUser 如果用户发起的进度更改是正确的。     */    @Override    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {        if (fromUser)  //如果user发出的操作是正确的那么 就将进度颜色改变成当前颜色        {            color = changeColor(progress);            if(listener != null)            {                listener.onProgressColorChanged(color);            }        }    }    /**     * onStartTrackingTouch 通知用户开始触摸手势了  用户可以使用该禁用进度条的推进     *     * @param seekBar seekBar的触摸手势开始     */    @Override    public void onStartTrackingTouch(SeekBar seekBar) {        color = changeColor(seekBar.getProgress());        if(listener != null)        {            listener.onStartColorChanged(color);        }    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {        color = changeColor(seekBar.getProgress());        if(listener != null)        {            listener.onStopColorChanged(color);        }    }    /**     * 创建进度条     * @param width     * @param height     */    private void createSeekBar(int width, int height)    {        //无颜色  蓝 绿  青色  橙色   品红   黄色   纯白        int[] mcolor = new int[] {0xFF000000,0xFF0000FF,0xFF00FF00,0xFF00FFFF,0xFF00FFFF,0xFFFF00FF,0xFFFF00FF,0xFFFFFF00,0xFFFFFFFF};        /**public GradientDrawable (GradientDrawable.Orientation orientation, int[] colors)         GradientDrawable  创建新的渐变可绘制给定一个方向和一个渐变的颜色的数组。         参数1:         GradientDrawable.Orientation BL_TR draw the gradient from the bottom-left to the top-right  绘制渐变从左下到右上角         GradientDrawable.Orientation BOTTOM_TOP draw the gradient from the bottom to the top绘制渐变从底部到顶部         GradientDrawable.Orientation BR_TL draw the gradient from the bottom-right to the top-left绘制渐变从右到左上方         GradientDrawable.Orientation LEFT_RIGHT draw the gradient from the left to the right绘制渐变从左到右         GradientDrawable.Orientation RIGHT_LEFT draw the gradient from the right to the left绘制渐变从右向左         GradientDrawable.Orientation TL_BR draw the gradient from the top-left to the bottom-right绘制渐变从左到右下角         GradientDrawable.Orientation TOP_BOTTOM draw the gradient from the top to the bottom从上到下的渐变绘制         GradientDrawable.Orientation TR_BL draw the gradient from the top-right to the bottom-left绘制渐变从右上方到左下方         参数2:   为颜色数组         */        GradientDrawable m = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,mcolor);        m.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置渐变类型        m.setCornerRadius(15); //指定矩形的角半径。        this.setProgressDrawable(m);    }    private int changeColor(int progress) {        int r = 0;        int g = 0;        int b = 0;        Log.i("progress", "progress       " + progress);        if (progress < 256) {            r = 255;            g = progress % 256;        } else if (progress < 256 * 2) {            g = 256;            r = 256 - progress % 256;        } else if (progress < 256 * 3) {            r = 0;            g = 255;            b = progress % 256;        } else if (progress < 256 * 4) {            b = 255;            g = 256 - progress % 256;        } else if (progress < 256 * 5) {            b = 255;            r = progress % 256;        } else if (progress < 256 * 6) {            r = 255;            b = 256 - progress % 256;        }        return Color.argb(255,r,g,b);    }}</span>

  

二、XML

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/RelativeLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.he_jingzhou.seekbarcolordemo.MainActivity">    <com.example.he_jingzhou.seekbarcolordemo.SeekBarColorPicker        android:id="@+id/SeekbarColorPicker"        android:maxHeight="7dp"        android:maxWidth="7dp"        android:thumb="@drawable/brightness_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout><span style="color:#ff0000;"></span></span>


三、MainActivity.Java


package com.example.he_jingzhou.seekbarcolordemo;<span style="font-size:18px;">import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.RelativeLayout;import android.widget.SeekBar;public class MainActivity extends AppCompatActivity {    SeekBar seekBar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SeekBarColorPicker seekBarColorPicker = (SeekBarColorPicker)findViewById(R.id.SeekbarColorPicker);        final RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);        seekBarColorPicker.setOnSeebarColorChangedListener(new SeekBarColorPicker.OnColorChangedListener() {            @Override            public void onProgressColorChanged(int color) {                //Log.i("", "   " +);                relativeLayout.setBackgroundColor(color);            }            @Override            public void onStartColorChanged(int color) {            }            @Override            public void onStopColorChanged(int color) {            }        });        setGradients(seekBarColorPicker, Color.RED);    }    /**     *GradientDrawable 创建新的渐变可绘制给定一个方向和一个渐变的颜色的数组。     * setGradients  设置   进度条的  渐变色     * @param bar     * @param color     */    public void setGradients(SeekBar bar,int color)    {                   //橙色  黄  绿  青色   蓝   品红   红        int[] mcolor= new int[]{0xFFFF0000,0xFFFFFF00,0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF,0xFFFF0000};        GradientDrawable m = new GradientDrawable(GradientDrawable.Orientation.BL_TR,mcolor);        m.setGradientType(GradientDrawable.LINEAR_GRADIENT);        m.setCornerRadius(15);        bar.setProgressDrawable(m);    }}</span>


结果表现





源码:http://download.csdn.net/detail/csdnhejingzhou/9389938

0 0
原创粉丝点击