我碰到了这个基本的drawRect,没有显示任何内容,我也不知道为什么.
onDraw
protected voID onDraw(Canvas canvas) { canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawCircle(circle1x, circle1y, circleRadius, circlePaint); canvas.drawCircle(circle2x, circle2y, circleRadius, circlePaint); canvas.drawRect(rect, rectPaint);}
setupCropPing
在onDraw之前运行,并从VIEw构造函数调用以设置所有var
private voID setupCropPing() { final float scale = getContext().getResources().getdisplayMetrics().density; circleRadius = (int) (circleRadiusDp * scale + 0.5f); displayMetrics metrics = new displayMetrics(); ((Activity) getContext()).getwindowManager().getDefaultdisplay().getMetrics(metrics); displayX = metrics.wIDthPixels; displayY = metrics.heightPixels; cropAreaY = displayY / 3; cropAreaX = displayX; //Setting up the circles for adjusting circle1x = displayX / 2; circle1y = displayY / 2 - (cropAreaY / 2); circle2x = displayX / 2; circle2y = displayY / 2 + (cropAreaY / 2); canvasPaint = new Paint(); canvasPaint.setcolor(0xffffff00); circlePaint = new Paint(); circlePaint.setcolor(0xffffff00); circlePaint.setAntiAlias(true); rectPaint = new Paint(); rectPaint.setcolor(0xffffff00); rect = new Rect(); rect.set(0, circle1y, 0, displayY - cropAreaY - circle1y);}
drawCircle可以正常工作并按我的期望进行绘制,我检查了赋予drawRect的数字,并按应有的方式设置了它们,因此我真的不知道这里可能出什么问题.
全视图类
package com.samplersnapshoot.domiq.samplersnapshoot;import androID.app.Activity;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.PorterDuff;import androID.graphics.Rect;import androID.util.AttributeSet;import androID.util.displayMetrics;import androID.util.Log;import androID.vIEw.VIEw;import java.io.file;import java.io.fileNotFoundException;import java.io.IOException;import java.io.RandomAccessfile;import java.nio.MappedByteBuffer;import java.nio.channels.fileChannel;import java.util.concurrent.CountDownLatch;/** * Created by domix on 14.8.2015.. */public class CropPingVIEw extends VIEw {public final String TAG = "DEBUG";private Canvas cropCanvas;private Bitmap canvasBitmap;private int displayX;private int displayY;private int circle1x = 0;private int circle2x = 0;private int circle1y = 0;private int circle2y = 0;private int circleRadiusDp = 20;private int circleRadius = 100;private int cropAreaX = 0;private int cropAreaY = 0;private Rect rect;private Paint canvasPaint;private Paint circlePaint;private Paint rectPaint;public CropPingVIEw(Context context, AttributeSet attrs){ super(context, attrs); setupCropPing();}private voID setupCropPing() { final float scale = getContext().getResources().getdisplayMetrics().density; circleRadius = (int) (circleRadiusDp * scale + 0.5f); displayMetrics metrics = new displayMetrics(); ((Activity) getContext()).getwindowManager().getDefaultdisplay().getMetrics(metrics); displayX = metrics.wIDthPixels; displayY = metrics.heightPixels; cropAreaY = displayY / 3; cropAreaX = displayX; //Setting up the circles for adjusting circle1x = displayX / 2; circle1y = displayY / 2 - (cropAreaY / 2); circle2x = displayX / 2; circle2y = displayY / 2 + (cropAreaY / 2); canvasPaint = new Paint(); canvasPaint.setcolor(0xffffff00); circlePaint = new Paint(); circlePaint.setcolor(0xffffff00); circlePaint.setAntiAlias(true); rectPaint = new Paint(); rectPaint.setARGB(50, 135, 225, 255);}/*@OverrIDeprotected voID onMeasure (int wIDthMeasureSpec, int heightmeasureSpec) { displayX = wIDthMeasureSpec; displayY = heightmeasureSpec; invalIDate(); super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);}*/@OverrIDeprotected voID onSizeChanged(int w, int h, int olDW, int oldh) { super.onSizeChanged(w, h, olDW, oldh); //Getting bitmap getPath myPath = new getPath(); final file myfile = myPath.getLastModifIEdfile(); final CountDownLatch latch = new CountDownLatch(1); Thread getCanvasBitmap = new Thread() { public voID run() { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDither = true; opt.inPreferredConfig = Bitmap.Config.ARGB_8888; int i = 0; while (canvasBitmap == null && ++i < 500) { System.gc(); Log.d(TAG, "Trying again: " + i); canvasBitmap = BitmapFactory.decodefile(myfile.getabsolutePath(), opt); } latch.countDown(); } }; getCanvasBitmap.start(); try { latch.await(); } catch (InterruptedException e) { e.printstacktrace(); } //Turning into mutable bitmap myfile.getParentfile().mkdirs(); RandomAccessfile randomAccessfile = null; try { randomAccessfile = new RandomAccessfile(myfile, "rw"); } catch (fileNotFoundException e) { e.printstacktrace(); } int bWIDth = canvasBitmap.getWIDth(); int bHeight = canvasBitmap.getHeight(); fileChannel channel = randomAccessfile.getChannel(); MappedByteBuffer map = null; try { map = channel.map(fileChannel.MapMode.READ_WRITE, 0, bWIDth*bHeight*4); } catch (IOException e) { e.printstacktrace(); } canvasBitmap.copyPixelsToBuffer(map); canvasBitmap.recycle(); this.canvasBitmap = Bitmap.createBitmap(bWIDth, bHeight, Bitmap.Config.ARGB_8888); map.position(0); this.canvasBitmap.copyPixelsFromBuffer(map); try { channel.close(); } catch (IOException e) { e.printstacktrace(); } try { randomAccessfile.close(); } catch (IOException e) { e.printstacktrace(); }}protected voID onDraw(Canvas canvas) { canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); Log.d(TAG, "display vars:" + displayX + " " + displayY); canvas.drawCircle(circle1x, circle1y, circleRadius, circlePaint); canvas.drawCircle(circle2x, circle2y, circleRadius, circlePaint); rect = new Rect(); rect.set(5, circle1y, displayX, displayY - cropAreaY - circle1y); canvas.drawRect(rect, rectPaint);}}
该类的代码编写得还很差,我还没有集成很多功能并对其进行清理.
解决方法:
rect.set(0, circle1y, 0, displayY - cropAreaY - circle1y);
您的左坐标和右坐标均为0.
它的
rect.set(int left, int top, int right, int bottom);
编辑:
好吧,我设法查明了这个问题.以下行在您的VIEw类中
rect.set(5, circle1y, displayX, displayY - cropAreaY - circle1y);
在您的代码中使用相同的计算,对于具有480×800显示屏的设备,我得到以下坐标值.
rect.set(5, 267, 480, 267);
同样,您的Rect侧面重叠;您的顶侧和底侧都在相同的Y坐标上.这将产生一个262像素宽和零高度的矩形.
您需要做的就是更新坐标计算并提供适当的坐标.否则,您的Rect应该画的很好.
总结以上是内存溢出为你收集整理的android drawRect没有画任何东西全部内容,希望文章能够帮你解决android drawRect没有画任何东西所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)