在onClick事件中放大ImageView图像

时间:2013-07-14 19:05:47

标签: java android eclipse adt

我是Java和Android的新手,所以请怜悯。我的活动中有3个按钮和imageView。每个按钮都会生成ImageView中显示的图片的新版本。当我点击它时,我希望imageView将当前图片放大到全屏。我的问题是:实现这一目标的最佳方法是什么?

  1. 我应该创建onClickListener来放大图片吗?
  2. 我应该定义一个方法,让ImageView在activity_start.xml中调用android:onClick="myMethodToCall

    我的activity_start.xml代码:

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="template1match"
        android:text="image1" />
    
    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_centerHorizontal="true"
        android:onClick="template2match"
        android:text="image2" />
    
    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/button2"
        android:layout_marginRight="16dp"
        android:onClick="template3match"
        android:text="image3" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:src="@drawable/wall" />
    

  3. 我的Start.java代码:

    package com.example.matchtemplate;
    
    import java.io.File;
    
    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.core.Core;
    import org.opencv.core.Core.MinMaxLocResult;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.Point;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class Start extends Activity {
    
        Button button1;
        Button button2;
        Button button3;
    
        ImageView imageView1;
    
        File cacheDir;
    
        protected static final String TAG = "OpenCV";
    
        private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
            public void onManagerConnected(int status) {
                switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG, "Open CV loaded successfully");
                    break;
    
                default:
                    super.onManagerConnected(status);
                    break;
                }
            };
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_start);
            //addListenerOnButton();
            initDir();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.start, menu);
            return true;
        }
    
        @Override
        public void onResume() {
            super.onResume();
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this,
                    mLoaderCallBack);
    
        }
    
        // ADDED THIS: to create/initialize external file
        // Be sure write permissions is enabled in the manifest file
        // ie add: <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/>
        public void initDir() {
            if (android.os.Environment.getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED)) {
                cacheDir = new File(
                        android.os.Environment.getExternalStorageDirectory(),
                        "FileList");
    
                if (!cacheDir.exists()) {
                    cacheDir.mkdirs();
                }
            }
        }
    
        // added this to simplify creating full file path
        public String getFileAbsPath(String fileName) {
            File f = new File(cacheDir, fileName);
            return f.getAbsolutePath();
        }
    
        public void template1match(View v) {
    
            imageView1 = (ImageView) findViewById(R.id.imageView1);
            button1 = (Button) findViewById(R.id.button1);
    
            String infile = getFileAbsPath("wall.jpg");
            String tp = getFileAbsPath("template1.jpg");
            String outFile = getFileAbsPath("result.jpg");
    
            try {
                matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
                Bitmap bm = BitmapFactory.decodeFile(outFile);
                imageView1.setImageBitmap(bm);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        public void template2match(View v) {
    
            imageView1 = (ImageView) findViewById(R.id.imageView1);
            button2 = (Button) findViewById(R.id.button2);
    
            String infile = getFileAbsPath("wall.jpg");
            String tp = getFileAbsPath("template2.jpg");
            String outFile = getFileAbsPath("result.jpg");
    
            try {
                matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
                Bitmap bm = BitmapFactory.decodeFile(outFile);
                imageView1.setImageBitmap(bm);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        public void template3match(View v) {
    
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        button3 = (Button) findViewById(R.id.button3);
    
        String infile = getFileAbsPath("wall.jpg");
        String tp = getFileAbsPath("template3.jpg");
        String outFile = getFileAbsPath("result.jpg");
    
        try {
            matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
            Bitmap bm = BitmapFactory.decodeFile(outFile);
            imageView1.setImageBitmap(bm);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
    
        public void matchTemplate(String inFile, String templateFile,
                String outFile, int match_method) {
            Log.i(TAG, "Running Template Matching");
    
            Mat img = Highgui.imread(inFile);
            Mat templ = Highgui.imread(templateFile);
    
            // / Create the result matrix
            int result_cols = img.cols() - templ.cols() + 1;
            int result_rows = img.rows() - templ.rows() + 1;
            Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
    
            // / Do the Matching and Normalize
            Imgproc.matchTemplate(img, templ, result, match_method);
            Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    
            // / Localizing the best match with minMaxLoc
            MinMaxLocResult mmr = Core.minMaxLoc(result);
    
            Point matchLoc;
            if (match_method == Imgproc.TM_SQDIFF
                    || match_method == Imgproc.TM_SQDIFF_NORMED) {
                matchLoc = mmr.minLoc;
            } else {
                matchLoc = mmr.maxLoc;
            }
    
            // / Show me what you got
            Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
                    matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
    
            // Save the visualized detection.
            Log.i(TAG, "Writing: " + outFile);
            Highgui.imwrite(outFile, img);
    
        }
    }
    

3 个答案:

答案 0 :(得分:0)

两者是等价的 - 所有与xml一起发生的事情是,幕后Android将为您创建一个onClickListener,注册它,并且该侦听器将调用xml中命名的函数。所以没有理由偏爱另一个。由于没有理由关心,我倾向于使用xml路由,因为它需要的代码稍少。

答案 1 :(得分:0)

我看到了Gabe的答案。但我会有自己的答案。尽管在xml的情况下Android会为你创建 onClicListener ,但两者都是一样的。但我更愿意去编码而不是xml,因为它会更容易理解。假设某人明天会看到你的代码,并且会点击图像并且图像会放大,他不会在代码中使用onClicklistener,所以可能会在他的第一眼看上去混淆。但如果他愿意的话他是一个很好的编码器,他会寻找xml,但如果它会在代码本身,那么它将更容易理解。

答案 2 :(得分:0)

如果我在你的位置,我会在点击图像后实施警报对话,并在警报对话框中有一个自定义的.xml,它有一个fill_parent imageview。因此,一旦单击图像,警报就会出现,内部图像变为点击的图像