如何让用户拖动和的LinearLayout内下降的图像 [英] How to allow user to drag and drop an image within a LinearLayout

查看:274
本文介绍了如何让用户拖动和的LinearLayout内下降的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的LinearLayout 设置其内部有图像。

I have a LinearLayout set up which has an image within it.

我的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>

它显示与它里面的方形箱(底部默认左)一个色谱(布局背景),为显示在以下

It displays a color spectrum (the layout background) with a square box (bottom left by default) inside it as shown in the following:


  • 如何让用户拖放小广场形象
    在布局中唯一的,也是获取X和Y值?

  • 无论何时,里面的布置在用户presses,广场移到
    布局内的坐标。我该怎么做?

  • 我想,然后使用这些X和Y值来获得HEX / RGB值。
    我将如何能够完成它?

如果有人能帮助我与任何一个或全部,我会真的AP preciate它。

If someone can help me with either or all, I would really really appreciate it.

推荐答案

我建议使用画布为你的形象,因为这将允许你绘制正方形选择颜色在色彩频谱。

I suggest using a Canvas for your image as this would allow you to draw the square to select the colour over the colour spectrum.

下code使用了覆盖onTouchEvent会给你能够给你在哪里,用户presses的坐标的画布。

The code below uses a canvas that overrides onTouchEvent and will give you be able to give you the coordinates of where the user presses.

 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.colour);
        square = BitmapFactory.decodeResource(c.getResources(), R.drawable.square);
    }

    @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("Colours","R:" +redValue +" B:" + blueValue + " G:" +greenValue);

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


        return true;
    }

}

要使用这个CanvasView类简单地做到这一点,或编辑为您自己的布局:

To use this CanvasView class simply do this or edit it for your own layouts:

    CanvasView canvasView = new CanvasView(this);
    LinearLayout linearlayout = (LinearLayout) findViewById(R.id.linearlayout);
    linearlayout.addView(canvasView);

最后,函数映射x和y为一种颜色。只需阅读坐标的像素颜色。

Finally the function to map the x and y to a colour. Simply read the pixel colour of the coordinates.

这篇关于如何让用户拖动和的LinearLayout内下降的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆