本帖最后由 采集的小蜜蜂 于 2020-11-11 17:46 编辑
取到的颜色不是现在我把ImageView调整到居中的颜色,而是以Imageview以左上角为坐标的颜色。
MainActivity.class[Java] 纯文本查看 复制代码 package com.ykd.bed.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.ykd.bed.R;
import com.ykd.bed.base.BaseActivity;
import com.ykd.bed.utils.DrawView;
import org.json.JSONArray;
import org.json.JSONException;
import butterknife.BindView;
public class RGBActivity extends BaseActivity implements View.OnTouchListener, View.OnClickListener {
@BindView(R.id.iv_back)
ImageView ivBack;
@BindView(R.id.colorpan)
ImageView colorpan;
@BindView(R.id.colortext)
TextView textView;
@BindView(R.id.rgblayout)
RelativeLayout relativeLayout;
private Bitmap bitmap;
@Override
protected String getClassName() {
return RGBActivity.class.getSimpleName();
}
@Override
protected int getViewId() {
return R.layout.activity_r_g_b;
}
@Override
protected void initView() {
ivBack.setOnClickListener(this);
bitmap = ((BitmapDrawable) colorpan.getDrawable()).getBitmap();
colorpan.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX()+100;
int y = (int) event.getY()+150;
try{
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(mContext,x+"...."+y+"",Toast.LENGTH_SHORT).show();
int color = bitmap.getPixel(x, y);
// 如果你想做的更细致的话 可以把颜色值的R G B 拿到做响应的处理
int r = Color.red(255-color);
int g = Color.green(255-color);
int b = Color.blue(255-color);
textView.setText( "r=" + r + ",g=" + g + ",b="
+ b);
textView.setTextColor(Color.rgb(r,g,b));
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
int color = bitmap.getPixel(x, y);
// 如果你想做的更细致的话 可以把颜色值的R G B 拿到做响应的处理
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
textView.setText( "r=" + r + ",g=" + g + ",b="
+ b);
}}catch (Exception e){
e.printStackTrace();
}
return true;
}
});
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.iv_back:
finish();
break;
}
}
}
activity_r_g_b.xml
[XML] 纯文本查看 复制代码 <?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rgblayout"
android:background="@mipmap/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:id="@+id/iv_back"
android:layout_centerVertical="true"
android:src="@mipmap/setting_back"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RGBLight"
android:textColor="@color/apptheme"
android:layout_centerInParent="true"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/colorpan"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:src="@mipmap/palette"/>
</RelativeLayout>
<TextView
android:id="@+id/colortext"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="30sp"/>
</RelativeLayout> |