Android Canvas.drawText

时间:2010-04-16 18:49:25

标签: android android-canvas drawtext

我有一个视图,我在onDraw(Canvas画布)方法中使用Canvas对象进行绘制。我的代码是:

Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);

paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);

问题是文字没有透过背景显示,我做错了什么?如果我删除canvas.drawPaint(paint)和paint.setColor(android.R.color.black),你可以看到屏幕上的文字......

2 个答案:

答案 0 :(得分:125)

解决了这个问题,事实证明android.R.color.black与Color.BLACK不同。将代码更改为:

Paint paint = new Paint(); 
paint.setColor(Color.WHITE); 
paint.setStyle(Style.FILL); 
canvas.drawPaint(paint); 

paint.setColor(Color.BLACK); 
paint.setTextSize(20); 
canvas.drawText("Some Text", 10, 25, paint); 

现在一切正常!!

答案 1 :(得分:19)

应注意documentation建议直接使用public void onResume() { super.onResume(); Intent intent =getActivity().getIntent(); pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0); intentFilter = new IntentFilter[] { }; nfcAdapter = NfcAdapter.getDefaultAdapter(getContext()); String action = intent.getAction(); boolean cardMatched=false; Tag tag=null; if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) { Toast.makeText(getContext(),"Blahhhhh",Toast.LENGTH_LONG).show(); tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); tappedCard=new Card(); tappedCard.setCardId(MainActivity.bytesToHexString(tag.getId())); //tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] techList = tag.getTechList(); ////////////+___________________________________________ ////////////+___________________________________________ if(tag == null){ Toast.makeText(getContext(), "tag == null",Toast.LENGTH_SHORT).show(); }else{ Card cc=new Card(); String tagId= MainActivity.bytesToHexString(tag.getId()); cc.setCardId(tagId); dbHelper=new DBHelper(getContext()); ArrayList<Card> checkList=dbHelper.getAllCards(); if(checkList.size()>0) { for(Card d:checkList) { if (cc.equals(d)) { cardMatched=true; Toast.makeText(getContext(), "Card Matched !!!!",Toast.LENGTH_SHORT).show(); showCardDetails(cc.getCardId()); break; } else { continue; } } if (!cardMatched) { imgBusCard.setVisibility(View.GONE); showDetails.setVisibility(View.VISIBLE); Toast.makeText(getContext(), "Card Not Matched !!!!",Toast.LENGTH_SHORT).show(); CustomDialogAddCard customDialogAddCard =new CustomDialogAddCard(getActivity(),"T",cc); customDialogAddCard.show(); showCardDetails(cc.getCardId()); } } else { Toast.makeText(getContext(), "No cards found !!!!",Toast.LENGTH_SHORT).show(); CustomDialogAddCard customDialogAddCard =new CustomDialogAddCard(getActivity(),"T",cc); customDialogAddCard.show(); showCardDetails(cc.getCardId()); } } }else{ Toast.makeText(getContext(), "onResume() : " + action, Toast.LENGTH_SHORT).show(); } } 而不是Layout。关于使用Canvas.drawText的完整答案是here,但我将在下面提供摘要。

StaticLayout

以下是自定义视图上下文中的更全面的示例:

enter image description here

String text = "This is some text.";

TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
textPaint.setColor(0xFF000000);

int width = (int) textPaint.measureText(text);
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
staticLayout.draw(canvas);