java - 图像对话框事件

标签 java android android-studio

我对我的 Java 代码有疑问。我想打开一个图像作为对话框,然后选择图像的颜色。

颜色选择器工作正常。 该对话框也可以正常工作。(图片正在弹出窗口中打开)

唯一的问题是当我在对话框中打开图像时,颜色选择器不再工作。

请帮忙?

代码!

public class MainActivity extends AppCompatActivity {
ImageView mImageView;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View mylayout = LayoutInflater.from(this).inflate(R.layout.activity_2,null);
    final ImageView myImage = (ImageView) mylayout.findViewById(R.id.imageView1);

    myImage.setDrawingCacheEnabled(true);
    myImage.buildDrawingCache(true);

    final Button button1 = (Button)  findViewById(R.id.button1);

    myImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
                try {

                bitmap = myImage.getDrawingCache();
                int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());

                //getting RGB values

                int r = Color.red(pixel);
                int g = Color.green(pixel);
                int b = Color.blue(pixel);


                //getting Hex value
                String hex = "#" + Integer.toHexString(pixel);

                //set background color of view
                //mColorView.setBackgroundColor(Color.rgb(r,g,b));
                System.out.println("r"+r+"g"+g+"b"+b);
                // Make the variable a global var easy way
                } catch(Exception e){
                    System.out.println("FOUT JONGUH");
                }


            }
            return true;
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.activity_2);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
        }
    });
}

}

主要 XML:

 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="427dp" />

第二个 XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/dirk" />

最佳答案

您需要在对话框上而不是在布局充气器上声明 ImageView 。你可以这样做;

Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_2);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final ImageView myImage = (ImageView) dialog.findViewById(R.id.imageView1); //here is the imageview
dialog.show();

myImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
                try {

                bitmap = myImage.getDrawingCache();
                int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());

                //getting RGB values

                int r = Color.red(pixel);
                int g = Color.green(pixel);
                int b = Color.blue(pixel);


                //getting Hex value
                String hex = "#" + Integer.toHexString(pixel);

                //set background color of view
                //mColorView.setBackgroundColor(Color.rgb(r,g,b));
                System.out.println("r"+r+"g"+g+"b"+b);
                // Make the variable a global var easy way
                } catch(Exception e){
                    System.out.println("FOUT JONGUH");
                }


            }
            return true;
        }
    });

然后将你的onTouchlistener放入button.onClickListener

关于java - 图像对话框事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884360/

相关文章:

java - StringJoiner 从每行的第一个位置删除定界符

java - Android Intent 创建对象的新实例

android - 从 Android 服务改造,当应用程序处于后台/关闭时回调失败

android - 找不到参数Gradle的方法classpath()

java - LibGDX - 将透视相机表示为四元数

java - 如何刷新 NetBeans 中的自定义 bean?

java - 执行请求 关注以编程方式创建的编辑文本

android - 减少 apk 大小 android

android - 如果我使用 Picasso 或 Glide 或 Volley 检索图像,是否需要提供不同尺寸的图像?

java - 错误 :java. lang.IllegalArgumentException:找到意外的类型标记 67