簡體   English   中英

Android如何在Canvas.drawColor中使用文本輸入

[英]Android How to use a Text Input in Canvas.drawColor

我有兩個問題可能需要您幫助,我是編程新手,這可能真的很容易,但是我找了幾天的答案卻無法獲得答案。 我正在做一個簡單的android應用程序,在其中您在純文本字段中輸入一個十六進制數,然后單擊按鈕創建畫布,並且您在畫布中看到該十六進制數的顏色(例如:0xff000000,您將得到黑色)很遺憾,我無法將字符串用作Canvas.drawcolor()中的整數變量,有人可以告訴我如何正確執行此操作,和/或為什么我做錯了嗎?

我的第二個問題是,如何在創建畫布時不使按鈕和文本字段從布局中消失的情況下使其顯示?

這是我的代碼,我創建了吐司以查看是否正確獲取了字符串:

public class MainActivity extends Activity implements TextWatcher{
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private ImageView mImageView;
    private EditText mHexadecimal;
    private String comments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHexadecimal = (EditText) findViewById(R.id.Hexadecimal);
        mHexadecimal.addTextChangedListener(this);

    }

public void onClick(View v) {
        mBitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        comments = mHexadecimal.getText().toString();
        Toast.makeText(getApplicationContext(), comments,Toast.LENGTH_LONG).show();
        mCanvas.drawColor(Integer.parseInt(comments,16));       
        mImageView = new ImageView(this);
        mImageView.setImageBitmap(mBitmap);
        setContentView(mImageView);
        }

好吧,您可以通過以下方法嘗試一下:

String color = "#FCFC0000"; //this is your color string
int colorCode = Integer.parseInt(string.replaceFirst("^#",""), 16);

然后嘗試設置此的ColorCodemCanvas

暫無
暫無

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

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