在画布上绘制位图并使用getPixel的问题

时间:2017-07-26 19:28:52

标签: android canvas bitmap

我正在制作一个简单的LED色彩控制应用程序,它通过蓝牙将数据发送到arduino,然后控制LED灯条。

我正在尝试实现色轮,然后使用getPixel选择要发送到arduino的颜色。我有很多问题让位图正确绘制和工作。我正在测试使用Android Studio预览并使用我的手机(HTC U11)在硬件上进行测试。

无法让这张图片正确绘制。这是我的画布。

package com.example.buledin.ledstripcontrolapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;


//we must extend View for simplicity
public class DrawableCanvas extends View {

    static Canvas c;
    private Paint BlueEdgePaint = new Paint();
    private Paint BlackEdgePaint = new Paint();
    ColourPoint point;
    private Bitmap colourWheelB;
    Rect rect;

    public DrawableCanvas(Context context) {
        super(context);
        init(null, null, 0);
        this.setWillNotDraw(false);

    }

    public DrawableCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public DrawableCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, 0);
    }

    // This is the initialisation routine.
    private void init(Context context, AttributeSet attrs, int defStyle) {
        BlueEdgePaint.setColor(Color.BLUE);
        BlueEdgePaint.setStrokeWidth(4);
        BlueEdgePaint.setStyle(Paint.Style.STROKE);
        BlueEdgePaint.setAntiAlias(true);

        BlackEdgePaint.setColor(Color.BLACK);
        BlackEdgePaint.setStrokeWidth(2);
        BlackEdgePaint.setStyle(Paint.Style.STROKE);
        BlackEdgePaint.setAntiAlias(true);


     //   colourWheel = getResources().getDrawable(R.drawable.color_wheel_730_no_outline);
        colourWheelB = BitmapFactory.decodeResource(context.getResources(), R.drawable.color_wheel_730_no_outline);
        point = new ColourPoint(0, 0);
        invalidate();

    }

    // Here is our DRAW LOOP.
    @Override
    public void onDraw(Canvas c) {
        //draw
        DrawableCanvas.c = c;
        super.onDraw(c);

        //draw wheel
        rect = new Rect(0, 0, c.getWidth(), c.getHeight());
        c.drawBitmap(colourWheelB, 0, 0, null);

        //selection circle
        c.drawCircle(point.getX(), point.getY(), 20, BlueEdgePaint);
        c.drawCircle(c.getWidth() / 2, c.getHeight() / 2, c.getHeight() / 2, BlackEdgePaint);

    }

    public void updatePoint(int x, int y) {
        point.setNewCoords(x, y);
        invalidate();
    }


    public static double distanceFromCentre(double x, double y) {
        double A = Math.abs(x - (c.getWidth() / 2));
        double B = Math.abs(y - (c.getHeight() / 2));

        double distance = Math.sqrt(A * A + B * B);
        return distance;
    }

    public int[] getRGBfromPoint(double x, double y ) {

        int pixel = colourWheelB.getPixel((int) x ,(int) y);
        int Red = Color.red(pixel);
        int Green = Color.green(pixel);
        int Blue = Color.blue(pixel);
        int[] ret = {Red, Green, Blue};
        Log.v("Coords:  ", ""+point.getX() + " " +  point.getY());
        Log.v("Color:", "Red = " + Red + " " + "Green = " + Green + "Blue = " + Blue);
        return ret;

    }

}

这是我的布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.buledin.ledstripcontrolapp.MainActivity"
    tools:showIn="@layout/activity_main">


    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="22dp"
        android:paddingBottom="10dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="10dp"
        android:progress="100" />

    <Button
        android:id="@+id/strobebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="73dp"
        android:text="Strobe" />

    <Button
        android:id="@+id/rainbowbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/strobebutton"
        android:layout_centerHorizontal="true"
        android:text="RAINBOW" />

    <Button
        android:id="@+id/offbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/rainbowbutton"
        android:text="Off" />

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:text="Null" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar"
        android:layout_alignParentStart="true"
        android:gravity="center"
        android:text="Brightness"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/rainbowbutton"
        android:text="Status:" />

    <TextView
        android:id="@+id/brightness"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="100" />


    <com.example.buledin.ledstripcontrolapp.DrawableCanvas
        android:id="@+id/canvas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_below="@+id/strobebutton"
        android:color="#66000000" />



</RelativeLayout>

Here is what it looks like on the phone

And here is what it looks like in the Android studio preview

请注意,对于色彩轮和“轮廓圆”,缩放都是错误的,出于美观原因我将其绘制在顶部。我不知道为什么缩放是完全错误的,但是当我在硬件测试中使用手机上的应用程序时,我基本上只能设置第一个屏幕截图中显示的颜色,所以我在3/4的轮子上输掉了。

这是真正的踢球者和真正令人头疼的问题:尽管屏幕上的图像看起来如此(我已经让它成功地在我的手机上绘制了一个完整的圆圈)我仍然只能选择颜色就像只剩下车轮的左上角1/4一样。

0 个答案:

没有答案