java - 如何设置 Canvas 使用 @dimen 指定尺寸

标签 java android android-canvas android-xml

我有以下 XML:

<LinearLayout
        android:id="@+id/llColorSpect"
        android:layout_width="match_parent"
        android:layout_height="@dimen/color_scheme_height"
        android:orientation="vertical"
        android:background="@drawable/colorspect"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/seek_bar_margin"
        android:layout_below="@+id/tvBGColor" >
        <RelativeLayout
            android:id="@+id/rlColorSpect"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <ImageView
                android:id="@+id/ivSquare"
                android:layout_width="@dimen/title_text_pad"
                android:layout_height="@dimen/title_text_pad"
                android:layout_alignParentBottom="true"
                android:scaleType="fitXY"
                android:src="@drawable/esquare" />
        </RelativeLayout>
    </LinearLayout>

显示以下输出:

enter image description here

我尝试设置一个 Canvas ,以便用户可以在布局周围拖动小方 block :

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class CanvasView extends View {

    private Bitmap bitmap;
    private Bitmap square;
    private float mScaleFactor = 1f;
    int x = 0;
    int y = 0;

    public CanvasView(Context c) {
        super(c);
        bitmap= BitmapFactory.decodeResource(c.getResources(), R.drawable.colorspect);
        square = BitmapFactory.decodeResource(c.getResources(), R.drawable.esquare);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.scale(mScaleFactor, mScaleFactor);
        canvas.drawBitmap(bitmap, 0, 0, null);
        canvas.drawBitmap(square, x, y, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        Log.d("x and y", "X: " + x + " Y: " + y);
        int pixel = bitmap.getPixel((int)x,(int) y);
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);
        Log.d("Colors","R:" +redValue +" B:" + blueValue + " G:" +greenValue);

        //Draw onto the square onto image
        this.x = (int) x;
        this.y = (int) y;
        invalidate();

        return true;
    }
}

我从主函数中调用上述代码,如下所示:

CanvasView canvasView = new CanvasView(this);
RelativeLayout rlDragDrop = (RelativeLayout) findViewById(R.id.rlColorSpect);
rlDragDrop.addView(canvasView);

布局没有保持上图所示的相同布局,而是更改如下:

enter image description here

  • 如何不断修改代码以使代码生成的第二个图像反射(reflect) XML 文件/第一个图像?
  • 此外,每当方 block 一直移动到顶部或左侧或底部或右侧时,应用 FC 就会显示 Y=0 或 X=0。我该如何解决这个问题?

我想要做的是,获取正方形的 X 和 Y 坐标并将其转换为 R、G、B 值。 (这是有效的,除了我上面遇到的两个问题)

最佳答案

您想要实现的看起来像一个颜色选择器。我确信,有很多解决方案:) Android Color Picker

关于您的代码,最好直接子类化 ImageView 而不是简单的 View。所以 ImageView 会为你绘制一个背景图像(所有 xml 属性都是有值(value)的),你只需担心你的方 block 。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(square, x, y, null);
}

你的第二个问题可能会因为canvas.scale(mScaleFactor, mScaleFactor);而出现,但我不确定:)实际上,如果你迫切需要缩放,最好使用Canvas#drawBitmap(Bitmap, Rect, RectF, Paint) 甚至 Canvas.save()Canvas.restore()

关于java - 如何设置 Canvas 使用 @dimen 指定尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21345740/

相关文章:

java - 谷歌地图 : dynamic canvas drawing in mapview

android - 将 android Canvas 保存为 svg

java - 图像到字节数组不一致

java - 在 OS X El Capitan 上运行 IBM Notes Java 应用程序会抛出 UnsatisfiedLinkError

Java - 如何将方法作为参数传递?

android - 在 Eclipse 中显示 Android SDK 管理器、Android 虚拟设备管理器按钮?

android - RN : Incompatable Android library

Java 无参数构造函数 : Throw impossible exception, 或有空 catch block ?

android - 如何防止在背面 fragment 导航中再次设置 View 模型

android - 如何为使用 Canvas : Android 绘制的内容设置 ontouch 监听器