2014-01-25 28 views
1

我有下面的XML:如何設置畫布使用@dimen尺寸爲

<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

我試圖建立一個畫布,所以我可以允許用戶拖動佈局周圍的小方塊:

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

  • 我應如何修改代碼生成由代碼生成的第二個映像以反映XML文件/第一個映像?
  • 此外,每次廣場都會一路走到頂部或左側或底部或右側,應用FC說Y = 0或X = 0。我如何解決這個問題?

我在找的是得到方形的X和Y座標並將其轉換爲R,G,B值。 (這是工作,除了我有兩個問題以上)

回答

0

你正試圖實現看起來像一個顏色選擇器。我敢肯定,那裏有很多解決方案:) Android Color Picker

關於你的代碼,最好直接子類化一個簡單的View視圖。所以ImageView會爲你繪製背景圖片(所有的xml屬性都是非常有價值的),你只會對你的方塊感到擔心。

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

你的第二個問題可能accure因爲canvas.scale(mScaleFactor, mScaleFactor);,但我不知道:)其實,如果你需要批判縮放,它可能是更好地使用Canvas#drawBitmap(Bitmap, Rect, RectF, Paint)甚至Canvas.save()Canvas.restore()