使用android中的搜索栏逐渐改变5个矩形的颜色a

时间:2015-10-16 19:23:27

标签: android android-layout android-activity android-seekbar

我的屏幕上有5个矩形,底部有一个搜索栏(见下面的图像链接)。我试图通过移动搜索栏逐渐改变5个矩形中的4个(除了中间右边的灰色)之外的颜色。我在RectAngle类中使用了drawRect方法来绘制5个矩形,但是我在不同的类(GloblaUIVariables类)中实例化它们以使它们全局化,但是当我将搜索栏向右移动时没有任何反应。我在做什么呢?谢谢你的帮助。

Screen Image

这是我的RectAngle类:

 public class RectAngle extends View {

        public RectAngle(Context context) {
            super(context);
        }
        public RectAngle(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public RectAngle(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }


        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            float x = getWidth();
            float y = getHeight();



            // Draw  the top left rectangle
            GlobalUIVariables.topLeftRect.setStyle(Paint.Style.FILL);
            GlobalUIVariables.topLeftRect.setColor(Color.parseColor("#66FFFF"));
           // canvas.drawPaint(topLeftRect);
            canvas.drawRect(0, 0, x / 2, y / 2, GlobalUIVariables.topLeftRect);


            //Draw the bottom left rectangle

            GlobalUIVariables.bottomLeftRect.setStyle(Paint.Style.FILL);
            //bottomLeftRect.setColor(Color.WHITE);
           // canvas.drawPaint(bottomLeftRect);
            GlobalUIVariables.bottomLeftRect.setColor(Color.parseColor("#FFFF00"));
            canvas.drawRect(0, y / 2, x / 2, y, GlobalUIVariables.bottomLeftRect);

            //Draw the top tight rectangle

            GlobalUIVariables.topRightRect.setStyle(Paint.Style.FILL);
            //topRightRect.setColor(Color.WHITE);
            GlobalUIVariables.topRightRect.setColor(Color.parseColor("#FF6600"));
            //canvas.drawPaint(topRightRect);
            canvas.drawRect(x / 2, 0, x, y / 3, GlobalUIVariables.topRightRect);

            // Draw the middle right rectangle

            GlobalUIVariables.midRightRect.setStyle(Paint.Style.FILL);
            GlobalUIVariables.midRightRect.setColor(Color.parseColor("#808080"));
           // canvas.drawPaint(midRightRect);
            canvas.drawRect(x / 2, y/3, x, 2*y / 3, GlobalUIVariables.midRightRect);

            //Draw the bottom right rectangle

            GlobalUIVariables.bottomRightRect.setStyle(Paint.Style.FILL);
            //bottomRightRect.setColor(Color.WHITE);
            GlobalUIVariables.bottomRightRect.setColor(Color.parseColor("#CCCC00"));
           // canvas.drawPaint(bottomRightRect);
            canvas.drawRect(x/2, 2*y/3, x, y, GlobalUIVariables.bottomRightRect);




        }
    }

这是我的主要活动类:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new RectAngle(this));
        setContentView(R.layout.activity_main);
        SeekBar seekBar;
        RectAngle rect;

        seekBar = (SeekBar)findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                RectAngle rect;
                rect = (RectAngle)findViewById(R.id.rectangle);
                rect.postInvalidate();
                GlobalUIVariables.topLeftRect.setStyle(Paint.Style.FILL);
                GlobalUIVariables.topLeftRect.setColor(Color.rgb(23 - progress, 200 + progress, 99 + progress));
                GlobalUIVariables.bottomLeftRect.setColor(Color.rgb(255, 133 + progress, 144 - progress));
                GlobalUIVariables.topRightRect.setColor((Color.rgb(88 + progress, 200 - progress, 177 + progress)));
                GlobalUIVariables.bottomRightRect.setColor(Color.rgb(230, 56 + progress, 233 - progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的GlobalUIVariables类:

public class GlobalUIVariables extends Application {
    public static Paint topLeftRect = new Paint();
    public static Paint bottomLeftRect = new Paint();
    public static Paint topRightRect = new Paint();
    public static Paint midRightRect = new Paint();
    public static Paint bottomRightRect = new Paint();
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity">
    <com.example.hassan.modernartui.RectAngle
        android:id="@+id/rectangle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar"/>

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="100"
        android:indeterminate="false"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="false"
        android:layout_alignParentEnd="false" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

从头开始设置绘画(创建视图时)。这样我们也可以摆脱使用应用程序类,GlobalUIVariables,用法。下面是如何将这些数据放入视图中,第2步将向您展示如何更改它们:

  1. 将涂料放入RectAngle类

    public class RectAngle extends View {
    
        public Paint topLeftRect, bottomLeftRect, topRightRect, midRightRect, bottomRightRect;
    
        public RectAngle(Context context) {
            super(context);
            initPaints();
        }
        public RectAngle(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public RectAngle(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initPaints();
        }
    
        void initPaints(){
            topLeftRect = new Paint();
            bottomLeftRect = new Paint();
            topRightRect = new Paint();
            midRightRect = new Paint();
            bottomRightRect = new Paint();
            topLeftRect.setStyle(Paint.Style.FILL);
            topLeftRect.setColor(Color.parseColor("#66FFFF"));
            bottomLeftRect.setStyle(Paint.Style.FILL);
            bottomLeftRect.setColor(Color.parseColor("#FFFF00"));
            topRightRect.setStyle(Paint.Style.FILL);
            topRightRect.setColor(Color.parseColor("#FF6600"));
            midRightRect.setStyle(Paint.Style.FILL);
            midRightRect.setColor(Color.parseColor("#808080"));
            bottomRightRect.setStyle(Paint.Style.FILL);
            bottomRightRect.setColor(Color.parseColor("#CCCC00"));
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            float x = getWidth();
            float y = getHeight();
            // Draw  the top left rectangle
            canvas.drawRect(0, 0, x / 2, y / 2, topLeftRect);
    
            //Draw the bottom left rectangle
            canvas.drawRect(0, y / 2, x / 2, y, bottomLeftRect);
    
            //Draw the top tight rectangle
            canvas.drawRect(x / 2, 0, x, y / 3, topRightRect);
    
            // Draw the middle right rectangle
            canvas.drawRect(x / 2, y/3, x, 2*y / 3, midRightRect);
    
            //Draw the bottom right rectangle
            canvas.drawRect(x/2, 2*y/3, x, y, bottomRightRect);
        }
    }
    
  2. 修复滑块侦听器以更改视图中的颜色(而不是应用程序类),然后调用invalidate

    final RectAngle rectAngleView = (RectAngle) findViewById(R.id.rectangle);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            rectAngleView.topLeftRect.setColor(Color.rgb(23 - progress, 200 + progress, 99 + progress));
            rectAngleView.bottomLeftRect.setColor(Color.rgb(255, 133 + progress, 144 - progress));
            rectAngleView.topRightRect.setColor((Color.rgb(88 + progress, 200 - progress, 177 + progress)));
            rectAngleView.bottomRightRect.setColor(Color.rgb(230, 56 + progress, 233 - progress));
            rectAngleView.invalidate();
        }
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
    
        }
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
    
        }
    });
    
  3. 希望这有帮助。

答案 1 :(得分:1)

  

我在这做什么呢。

在您设置新颜色后,您忘记获取对RectAngle的引用(您可以使用findViewById())并在其上调用View.invalidate()。这将安排重新绘制您的ViewonDraw将再次调用。在这里,您可以找到invalidate()的文档。 正如@Petey正确指出的那样,在onDraw中,您正在Paint对象中重复重置

答案 2 :(得分:1)

您是否已将自定义矩形视图添加到activity_main布局?

如果您尚未添加该视图,则应将其添加到activity_main布局中,如此

<com.blah.blah.RectAngle
         android:id="@+id/rectangle_view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

获取RectAngle视图的句柄并在RectAngle视图上调用invalidate以在onStopTrackingTouch上重绘。

<强>答案:

在Rectangle onDraw方法中,它使用您第一次定义的颜色。注释那些或在Rectangle视图构造函数中设置它们或创建活动的方法。

GlobalUIVariables.topLeftRect.setColor(Color.parseColor("#66FFFF"));

在onDrawMethod中注释掉所有这些颜色后,在onProgressChanged上的Rectangle上调用invalidate方法就像现在这样。