Android 图片浏览 放大图片

效果图:
在这里插入图片描述

布局文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加透明度"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减少透明度"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张"
            android:layout_weight="1"/>

    </LinearLayout>

    <!--此处显示图片整体-->
    <ImageView
        android:id="@+id/imagel"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:src="@drawable/xiaochouyu"
        android:scaleType="fitCenter"/>

    <ImageView
        android:id="@+id/image2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00f"
        android:layout_margin="5dp"/>


</LinearLayout>

代码实现透明度改变:

public class MainActivity extends AppCompatActivity {

    //定义一个访问图片的数组
    int[] images = new int[]{
            R.drawable.xiaochouyu ,
            R.drawable.leidayu ,
            R.drawable.paodangyu ,
            R.drawable.huangjindiao ,
            R.drawable.piaopiao
    };

    //定义默认显示的图片
    int currentImg = 2 ;
    //定义图片初始透明度
    private int alpha = 255 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button plus = (Button) findViewById(R.id.plus) ;
        final Button minus = (Button) findViewById(R.id.minus) ;
        final Button next = (Button) findViewById(R.id.next) ;
        final ImageView imageView01 = (ImageView) findViewById(R.id.imagel);
        final ImageView imageView02 = (ImageView) findViewById(R.id.image2);
        //定义查看下一张图片的监听器
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示下一张图片
                imageView01.setImageResource(images[currentImg++ % images.length]);
            }
        });
        //定义改变图片透明度的方法
        View.OnClickListener listener = new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                if (v == plus){
                    alpha = alpha + 20 ;
                }
                if (v == minus){
                    alpha = alpha - 20 ;
                }
                if (alpha >= 255){
                    alpha = 255 ;
                }
                if (alpha <= 0){
                    alpha = 0 ;
                }
                imageView01.setImageAlpha(alpha);
            }
        };
        //为两个按钮添加监听器
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
        imageView01.setOnTouchListener(new View.OnTouchListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView01.getDrawable();
                //获取第一个托片显示框中的位图
                Bitmap bitmap = bitmapDrawable.getBitmap();
                //bitmap图片实际大小与第一个Imageview的缩放比例
                double scale = 1.0 * bitmap.getHeight() / imageView01.getHeight();
                //获取需要显示的图片开始点
                int x = (int) (event.getX() * scale);
                int y = (int) (event.getY() * scale);
                if (x + 120 > bitmap.getWidth()){
                    x = bitmap.getWidth() - 120 ;
                }
                if (y + 120 > bitmap.getHeight()){
                    y = bitmap.getHeight() - 120 ;
                }
                //显示图片的指定区域
                imageView02.setImageBitmap(Bitmap.createBitmap(bitmap , x , y , 120 , 120));
                imageView02.setImageAlpha(alpha);
                return false;
            }
        });
    }
}
发布了160 篇原创文章 · 获赞 217 · 访问量 8万+
展开阅读全文

android获取预览图片怎么获取一张图片

06-19

public class MainActivity extends AppCompatActivity { private SurfaceView sv_camera_surfaceview; private android.hardware.Camera camera; private SurfaceHolder surfaceholder; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sv_camera_surfaceview = (SurfaceView) findViewById(R.id.sv_camera_sufaceview); sv_camera_surfaceview.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open();//开启摄像头 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Camera.Parameters parameters = camera.getParameters(); parameters.setPictureFormat(ImageFormat.NV21); parameters.setPreviewFormat(ImageFormat.NV21); //parameters.setPictureFormat(ImageFormat.NV21); //设置格式 //parameters.getSupportedPictureSizes(); //parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//自动聚焦 //parameters.setPreviewSize(900,240); /* WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); int screenWidth = screenWidth = display.getWidth(); int screenHeight = screenHeight = display.getHeight(); parameters.setPictureSize(screenWidth, screenHeight);*/ //camera.Size(100,50); // parameters.setPictureSize(500,80); // parameters.set("YCbCr_420_SP",100); //List<android.support.annotation.Size>picturesize=camera.getParameters().getSupportedPictureSizes(); // List<Camera.Size> picturesize=camera.getParameters().getSupportedPictureSizes(); /*List<Camera.Size>picturesize=camera.getParameters().getSupportedPictureSizes(); List<Camera.Size>previewsize=camera.getParameters().getSupportedPreviewSizes(); List<Integer> previewformats=camera.getParameters().getSupportedPreviewFormats(); List<Integer> previewframerates=camera.getParameters().getSupportedPreviewFrameRates(); //Lise<Integer> Size size=null; for (int i=0 ;i<picturesize.size();i++){ size= (Size) picturesize.get(i); } for (int i=0;i<previewsize.size();i++){ size= (Size) previewsize.get(i); } Integer in=null; for (int i=0;i<previewformats.size();i++){ in=previewformats.get(i); } parameters.setPictureSize(1080,1920); //camera.setParameters(parameters); // camera.startPreview(); // int previewWidth; // int previewHeight; //List<Camera.Size>previerSizes=camera.getParameters().getPreviewSize();*/ List<Camera.Size> pictureSize = camera.getParameters().getSupportedPictureSizes(); List<Camera.Size> previewsize = camera.getParameters().getSupportedPreviewSizes(); //List<int []> range=parameters.getSupportedPreviewFpsRange(); Camera.Size size; // size.width=width; // size.height=height; for (int i = 0; i < pictureSize.size(); i++) { size = pictureSize.get(i); } for (int i = 0; i < previewsize.size(); i++) { size = previewsize.get(i); } /*for (int j=0;j<range.size();j++){ int [] r=range.get(j); }*/ //parameters.setPreviewFpsRange(30,30); parameters.setPictureSize(192,1080); parameters.setPreviewSize(1920,1080); //parameters.setPreviewFrameRate(30000); camera.setDisplayOrientation(0);//角度 camera.setParameters(parameters); try { camera.setPreviewDisplay(sv_camera_surfaceview.getHolder());//实时预览 camera.startPreview(); /// /开启预览 } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } public void takephoto(View view) { camera.setPreviewCallback(new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { //byte a=data[0]; //if (data.length<=1) { //System.arraycopy(data, 0, ImageFormat.NV21, 0, data.length); FileOutputStream fileoutputstream ; try { fileoutputstream = new FileOutputStream(String.format("sdcard/DCIM/camera/" + System.currentTimeMillis() + "12")); fileoutputstream.write(data); fileoutputstream.close(); //YuvImage yuvImage=new YuvImage(data,ImageFormat.NV21,1080,1920,null); //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } catch (IOException e) { e.printStackTrace(); } } }); } } 现在每次点击一次拍照都是获得很多张照片怎么只获得点击拍照最近的照片 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览