android drawRect没有绘制任何东西

时间:2015-08-18 21:04:39

标签: android canvas drawrect

我碰到了一堵墙,这个基本的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);

}

/*@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);


}
}

这个类远没有很好的编码,我还没有整合很多功能并清理它。

1 个答案:

答案 0 :(得分:4)

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

您的leftright坐标都是0.去图。

这是

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

编辑:

好的,我设法找出问题所在。以下行位于View

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

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

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

同样,您Rect的重叠边重叠;您的topbottom边都在同一个Y坐标上。这将生成262像素宽且零高度的矩形。

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