如何使用布局的背景作为位图,以X和Y转换为RGB值 [英] How to use a layout's background as a bitmap to convert X and Y to RGB values
问题描述
我有调色板为背景的布局。
在布局我添加为拇指较小的图像(这是使用调整 @dimen
来使其变小,真的很小,像一个十字),它应该走动围绕上述布局用户拖动:
我如何使用的布局的背景位图,所以我可以使用下面的code:
F =(的FrameLayout)findViewById(R.id.fl);
f.setOnTouchListener(FLT);IV =(ImageView的)findViewById(R.id.iv);View.OnTouchListener FLT =新View.OnTouchListener(){ @覆盖
公共布尔onTouch(视图V,MotionEvent事件){ 浮X = event.getX();
浮Y = event.getY();
// INT像素= resizedbitmap.getPixel((INT)X,(INT)Y); //布局的背景放在这里... 开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
如果(X℃,){
X = X + 10;
iv.setX(X);
iv.setY(Y);
}
如果(X GT; f.getWidth()){
X = X - 10;
iv.setX(X);
iv.setY(Y);
}
其他{
iv.setX(X);
iv.setY(Y);
}
//写您的code执行下来的动作
打破;
案例MotionEvent.ACTION_MOVE:
如果(X℃,){
X = X + 10;
iv.setX(X);
iv.setY(Y);
INRED = Color.red(像素);
inBlue = Color.blue(象素);
inGreen = Color.green(象素);
Log.d(颜色,R+ INRED +G:+ inGreen +B:+ inBlue);
}
如果(X GT; f.getWidth()){
X = X - 10;
iv.setX(X);
iv.setY(Y);
}
其他{
iv.setX(X);
iv.setY(Y);
}
//写您的code上执行contineus触摸移动的动作
打破;
案例MotionEvent.ACTION_UP:
//写您的code执行触摸一个行动
打破;
}
// TODO自动生成方法存根
返回true;
}
};
的XML是:
<的FrameLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@绘制/调色板2
机器人:ID =@ + ID / FL> < ImageView的
机器人:ID =@ + ID / IV
机器人:layout_width =10dp
机器人:layout_height =10dp
机器人:SRC =@绘制/ esquare/>< /&的FrameLayout GT;
我所希望做的是,创造一个颜色选择器,以便在用户的布局中拖动,用户与R G B值psented $ P $。谁能帮我完成codeS?
-
像你注释掉的一行获取像素。使用位图的与getPixel(X,Y)。你需要保持周围的位图这一点。
-
与getPixel()返回一个颜色INT,你可以通过检查的 http://developer.android.com/reference/android/graphics/Bitmap.html
实际上,文档错误地认为,getPixel返回android.graphics.Color对象。它不是。它返回一个颜色INT的argb。 -
获取颜色INT,C的成分通过这样的电话,你可以通过检查的 http://developer.android.com/reference/android/graphics/Color.html
INT阿尔法= Bitmap.alpha(C);
INT红色= Bitmap.red(C);
或者你可以自己做位OPS:
INT阿尔法= C>>> 24;
INT红色=(c取代;>> 16)及0xFF的;
这是否回答你的问题?
I have a layout with the color palette as the background.
Within the layout I added a smaller image as a thumb (which is resized using @dimen
to make it smaller, really small, like a crosshair) which should move around as the user drag around the layout above:
How do I use the layout's background as a bitmap so I can use the following code:
f = (FrameLayout) findViewById(R.id.fl);
f.setOnTouchListener(flt);
iv = (ImageView) findViewById(R.id.iv);
View.OnTouchListener flt = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
//int pixel = resizedbitmap.getPixel((int)x,(int) y); //the background of the layout goes here...
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x<0) {
x = x + 10;
iv.setX(x);
iv.setY(y);
}
if (x>f.getWidth()) {
x = x - 10;
iv.setX(x);
iv.setY(y);
}
else {
iv.setX(x);
iv.setY(y);
}
// Write your code to perform an action on down
break;
case MotionEvent.ACTION_MOVE:
if (x<0) {
x = x + 10;
iv.setX(x);
iv.setY(y);
inRed = Color.red(pixel);
inBlue = Color.blue(pixel);
inGreen = Color.green(pixel);
Log.d("Colors","R:" +inRed +" G:" +inGreen+" B:" + inBlue);
}
if (x>f.getWidth()) {
x = x - 10;
iv.setX(x);
iv.setY(y);
}
else {
iv.setX(x);
iv.setY(y);
}
// Write your code to perform an action on contineus touch move
break;
case MotionEvent.ACTION_UP:
// Write your code to perform an action on touch up
break;
}
// TODO Auto-generated method stub
return true;
}
};
The XML is:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/palette2"
android:id="@+id/fl" >
<ImageView
android:id="@+id/iv"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/esquare" />
</FrameLayout>
What I am looking to do is, create a color picker so as the user drags within the layout, the user is presented with the R G B values. Can anyone help me complete the codes?
Get the pixel with a line like the one you commented out. Use BitMap's getPixel(x,y). You need to keep a bitmap around for this.
getPixel() returns a color int, as you can see by checking http://developer.android.com/reference/android/graphics/Bitmap.html Actually, the documentation wrongly suggests that getPixel returns an android.graphics.Color object. It does not. It returns a color int argb.
get the components of the color int, c, by calls like this, as you can see by checking http://developer.android.com/reference/android/graphics/Color.html
int alpha = Bitmap.alpha(c);
int red = Bitmap.red(c);
Or you can do the bit ops yourself:
int alpha = c >>> 24;
int red = (c >>> 16) & 0xFF;
Does that answer your question?
这篇关于如何使用布局的背景作为位图,以X和Y转换为RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!