簡體   English   中英

不確定如何完美使用GradientDrawable

[英]Not sure how to use GradientDrawable perfectly

我在一項活動中需要一些輸入來制作可繪制的形狀。 這是一個小例子:

GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.RED);
gd.setCornerRadius(12);
preview.getLayoutParams().width = 100;
preview.getLayoutParams().height = 100;
preview.setBackground(gd);

但是,當我想將其實現為微調器時,必須多次選擇所有選項,以便獲得結果。 並且還有一個按鈕應顯示預覽,不起作用。

這是我的代碼:

import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import yuku.ambilwarna.AmbilWarnaDialog;

public class CustomizationActivity extends AppCompatActivity {

int currentColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customization);

    final EditText widthText = findViewById(R.id.width);
    final EditText heightText = findViewById(R.id.height);
    final Spinner shapeSpinner = findViewById(R.id.shape);
    final View colorView = findViewById(R.id.color);
    final View preview = findViewById(R.id.preview);
    final EditText size = findViewById(R.id.size);
    final EditText corners = findViewById(R.id.corners);
    Button button = findViewById(R.id.button);

    final GradientDrawable gd = new GradientDrawable();

    shapeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (position == 1) {

                gd.setShape(GradientDrawable.RECTANGLE);
                gd.setCornerRadius(Integer.parseInt(corners.getText().toString()));
                preview.getLayoutParams().width = Integer.parseInt(widthText.getText().toString());
                preview.getLayoutParams().height = Integer.parseInt(heightText.getText().toString());


            }

            if (position == 2) {

                gd.setShape(GradientDrawable.OVAL);

                preview.getLayoutParams().width = Integer.parseInt(size.getText().toString());
                preview.getLayoutParams().height = Integer.parseInt(size.getText().toString());

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    colorView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AmbilWarnaDialog dialog = new AmbilWarnaDialog(CustomizationActivity.this, currentColor, false, new AmbilWarnaDialog.OnAmbilWarnaListener() {
                @Override
                public void onCancel(AmbilWarnaDialog dialog) {

                }

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {

                    currentColor = color;

                    colorView.setBackgroundColor(color);

                    gd.setColor(color);

                }
            });

            dialog.show();

        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            preview.setBackground(gd);
        }
    });

}

}

它以某種方式起作用,但是我不知道如何糾正它。 我想要的是-如果可能的話-更改值時直接顯示預覽。 -如果沒有,則在編輯所有內容后,單擊按鈕即可獲得結果。

我不知道,但我認為問題出在微調器上。

有什么幫助嗎?

設置屬性調用gd.invalidateSelf();之后,嘗試調用onclick()方法gd.invalidateSelf();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM