几个月前我一直面临这个问题,我找不到任何关于它的事情。
我有一个使用canvas.drawPoint函数的应用程序,它永远不会在android 4.4或5.0或更高版本中绘制任何内容。
但它完美地工作在2.3到4.2之间,我实际上测试了不同的真实设备。
虚拟设备也一样。
问题仅在于点,因为使用canvas.drawRect或canvas.drawLine绘制矩形或线条即使在同一个应用程序中也能完美地工作。
示例:
canvas.drawRect(0, 0, 400, 400, paint); // working for all devices
for (int ii=0;ii<500+1;ii++)
{
for (int jj=0;jj<500+1;jj++)
{ canvas.drawPoint(ii, jj, paint); //not working for higher than 4.4 android version
} }
这将绘制第一个400 * 400矩形,但不会绘制第二个500 * 500系列的点。
对于我的真实应用程序,它使用了2个类,我实际上删除了一些计算,而oncliklistners只是为了简单但不起作用。
DrawView.java
package com.example.hazim.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawView extends View {
int pr=150;int pry=150; int it=23; double cx=-0.1;double cy=0.65;
Paint paint = new Paint();
public DrawView(Context context, AttributeSet att) {
super(context, att); //set the color in the creator of drawview
paint.setARGB(255, 150,150,150);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, 100, 100, paint); // as a test the drawview display
double prr=pr; double pryy=pry;
for (int ii=0;ii<pr+1;ii++) //pass the screen pixels in 2 directions
{
for (int jj=0;jj<pry+1;jj++)
{
canvas.drawPoint(ii, jj, paint);
}
}
}
//parameter setters
public void setpr(int a) {pr=a;}
public void setpry(int a) {pry=a;}
public void setcx(double a) {cx=a;}
public void setcy(double a) {cy=a;}
}
这是MainActivity.java
package com.example.hazim.myapplication;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
DrawView drawView=null; //instanciation of drawview
Button bouton=null;
EditText cx=null;
EditText cy=null;
EditText pr=null;
EditText pry=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // display activity main XML
drawView = (DrawView)findViewById(R.id.drawView1); // load the drawView
bouton = (Button)findViewById(R.id.button1); // load the button
cx = (EditText)findViewById(R.id.EditText02); // load the 4 editTexts
cy = (EditText)findViewById(R.id.EditText01);
pr = (EditText)findViewById(R.id.EditText03);
pry = (EditText)findViewById(R.id.EditText04);
}
}
答案 0 :(得分:2)
您没有正确配置Paint
。你必须设置适当的笔画宽度(Paint#setStrokeWidth),告诉画布你的观点有多大。否则它将是1px大,这在高分辨率显示器上很小,所以你甚至看不到它。
此外,由于您使用此绘制来绘制矩形 - 我将创建单独的Paint对象,仅用于绘制点:
public class DrawView extends View {
....
Paint rectPaint = new Paint();
Paint pointPaint = new Paint();
public DrawView(Context context, AttributeSet att) {
super(context, att); //set the color in the creator of drawview
rectPaint.setAntialias(true);
rectPaint.setARGB(255, 150,150,150);
pointPaint.setAntialias(true);
pointPaint.setColor(Color.RED); //your point color goes here
pointPaint.setStrokeWidth(getResources().getDimension(R.dimen.point_size)); //assuming you specified point size
//in dp in dimes.xml resource file
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, 100, 100, rectPaint);
double prr=pr; double pryy=pry;
for (int ii=0;ii<pr+1;ii++) //pass the screen pixels in 2 directions
{
for (int jj=0;jj<pry+1;jj++)
{
canvas.drawPoint(ii, jj, pointPaint);
}
}
}
}