我的屏幕上有5个矩形,底部有一个搜索栏(请参见下面的图像链接)。我试图通过移动搜索栏逐渐更改5个矩形中的4个(除灰色int中间右方)的颜色。我在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>

最佳答案

从一开始就设置油漆(创建视图时)。这样,我们还可以摆脱使用应用程序类GlobalUIVariables的用法。这里介绍了如何将这些数据放入视图中,第2步将向您展示如何更改它们:


将油漆放在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);
    }
}

修复滑块侦听器,以更改视图(而非应用程序类)中的颜色,然后调用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) {

    }
});



希望这可以帮助。

09-28 09:58