android drawRect 不绘制任何东西

android drawRect not drawing anything

我已经 运行 撞墙了,这个基本的 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);
}

设置裁剪

在 onDraw 之前 运行ning 并从 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 的数字并且它们被设置为它们应该的所以我真的不知道这里可能出了什么问题。

全景class

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);

}

/*@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
    displayX = widthMeasureSpec;
    displayY = heightMeasureSpec;
    invalidate();
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}*/

@Override
protected 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);


}
}

class 的编码还很差,我还没有集成很多功能并清理它。

rect.set(0, circle1y, 0, displayY - cropAreaY - circle1y);

你的 leftright 坐标都是 0。看图。

rect.set(int left, int top, int right, int bottom);

编辑:

好的,我设法查明了问题所在。以下行在您的 View class

rect.set(5, circle1y, displayX, displayY - cropAreaY - circle1y);

在您的代码中使用相同的计算,对于具有 480x800 显示屏的设备,我得到以下坐标值。

rect.set(5, 267, 480, 267);

同样,您的 Rect 有重叠的边; topbottom 两边都在同一个 Y 坐标上。这将产生一个 262 像素宽和 零高度.

的矩形

您需要做的就是更新您的坐标计算并提供正确的坐标。否则,你的 Rect 应该画得很好。