图像视图中的图像重叠

时间:2017-10-20 11:28:58

标签: android xml layout

我想将3张图像显示在这些内部透明的帧中。但我使用了3个图像视图来显示我的图片。问题是它们在帧中并不精确,但它们进入下一帧以填充图像视图的宽度和高度。这里我附加我的xml文件以及java文件。我不知道该使用什么,以便我的图像可以修复。掩蔽是选项还是别的什么?

     <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
       >


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true">

        </android.support.v7.widget.RecyclerView>


        <RelativeLayout
            android:id="@+id/imgContainer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_above="@+id/recycler_view"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/save_layout">

            <ImageView
                android:id="@+id/leftside"
                android:layout_width="128dp"
                android:layout_height="320dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:scaleType="matrix" />

            <ImageView
                android:id="@+id/middle"
                android:layout_width="128dp"
                android:layout_height="320dp"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:layout_toRightOf="@+id/leftside"
                android:scaleType="matrix" />


            <ImageView
                android:id="@+id/rightside"
                android:layout_width="128dp"
                android:layout_height="320dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:scaleType="matrix" />

            <ImageView
                android:id="@+id/imgframe"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true" />


        </RelativeLayout>


    </RelativeLayout>


import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import droidninja.filepicker.FilePickerBuilder;
import droidninja.filepicker.FilePickerConst;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class DisplayActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    public static final int RC_WRITE_PERMISSION = 12;
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    private static final int EXTERNAL_CAMERA_PERMISSION_CONSTANT = 100;
    protected static InterstitialAd mInterstitialAd;
    RecyclerView horizontal_recycler_view;
    HorizontalAdapter horizontalAdapter;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    PointF startPoint = new PointF();
    PointF midPoint = new PointF();
    float oldDist = 1f;
    int mode = NONE;
    ImageView leftside, rightside, middleside;

    String currentImage = "";
    RelativeLayout imgContainer;
    AlertDialog.Builder builder;
    ImageView mImage;
    File sdImageMainDirectory;
    int click = 0;
    ArrayList<String> filePaths = new ArrayList<>();
    ArrayList<Spacecraft> spacecrafts = new ArrayList<>();
    private float[] lastEvent = null;
    private float d = 0f;
    private float newRot = 0f;
    private int REQUEST_CAMERA = 1, SELECT_FILE = 5;
    private String userChoosenTask;
    private boolean left, right, middle = false;
    private int leftClickCount = 0;
    private int rightClickCount = 0;
    private int middleClickCount = 0;

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

        horizontal_recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
        horizontalAdapter = new HorizontalAdapter(Utils.sThumbs, this);

        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(DisplayActivity.this, LinearLayoutManager.HORIZONTAL, false);
        horizontal_recycler_view.setLayoutManager(horizontalLayoutManager);
        horizontal_recycler_view.setAdapter(horizontalAdapter);


        Bundle bdl = getIntent().getExtras();
        int index = bdl.getInt("Index");
        mImage = (ImageView) findViewById(R.id.imgframe);
        mImage.setImageResource(Utils.mThumbIds[index]);


        imgContainer = (RelativeLayout) findViewById(R.id.imgContainer);

        leftside = (ImageView) findViewById(R.id.leftside);
        rightside = (ImageView) findViewById(R.id.rightside);
        middleside = (ImageView) findViewById(R.id.middle);


        rightside.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                ImageView rightside = (ImageView) v;
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        savedMatrix.set(matrix);
                        startPoint.set(event.getX(), event.getY());
                        mode = DRAG;
                        lastEvent = null;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist = spacing(event);
                        if (oldDist > 10f) {
                            savedMatrix.set(matrix);
                            midPoint(midPoint, event);
                            mode = ZOOM;
                        }
                        lastEvent = new float[4];
                        lastEvent[0] = event.getX(0);
                        lastEvent[1] = event.getX(1);
                        lastEvent[2] = event.getY(0);
                        lastEvent[3] = event.getY(1);
                        d = rotation(event);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (rightClickCount == 0) {


                            right = true;
                            left = false;

                        }
                    case MotionEvent.ACTION_POINTER_UP:
                        mode = NONE;
                        lastEvent = null;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            // ...
                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - startPoint.x,
                                    event.getY() - startPoint.y);
                        } else if (mode == ZOOM && event.getPointerCount() == 2) {
                            float newDist = spacing(event);
                            matrix.set(savedMatrix);
                            if (newDist > 10f) {
                                float scale = newDist / oldDist;
                                matrix.postScale(scale, scale, midPoint.x,
                                        midPoint.y);
                            }
                            if (lastEvent != null) {
                                newRot = rotation(event);
                                float r = newRot - d;
                                matrix.postRotate(r, rightside.getMeasuredWidth() / 2,
                                        rightside.getMeasuredHeight() / 2);
                            }
                        }
                        break;
                }

                rightside.setImageMatrix(matrix);

                return true;
            }

            private float rotation(MotionEvent event) {
                double delta_x = (event.getX(0) - event.getX(1));
                double delta_y = (event.getY(0) - event.getY(1));
                double radians = Math.atan2(delta_y, delta_x);

                return (float) Math.toDegrees(radians);
            }

            @SuppressLint("FloatMath")
            private float spacing(MotionEvent event) {
                float x = event.getX(0) - event.getX(1);
                float y = event.getY(0) - event.getY(1);
                return (float) Math.sqrt(x * x + y * y);
            }

            private void midPoint(PointF point, MotionEvent event) {
                float x = event.getX(0) + event.getX(1);
                float y = event.getY(0) + event.getY(1);
                point.set(x / 2, y / 2);
            }
        });


        middleside.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                ImageView middleside = (ImageView) v;
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        savedMatrix.set(matrix);
                        startPoint.set(event.getX(), event.getY());
                        mode = DRAG;
                        lastEvent = null;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist = spacing(event);
                        if (oldDist > 10f) {
                            savedMatrix.set(matrix);
                            midPoint(midPoint, event);
                            mode = ZOOM;
                        }
                        lastEvent = new float[4];
                        lastEvent[0] = event.getX(0);
                        lastEvent[1] = event.getX(1);
                        lastEvent[2] = event.getY(0);
                        lastEvent[3] = event.getY(1);
                        d = rotation(event);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (middleClickCount == 0) {


                            right = false;
                            left = false;
                            middle = true;

                        }
                    case MotionEvent.ACTION_POINTER_UP:
                        mode = NONE;
                        lastEvent = null;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            // ...
                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - startPoint.x,
                                    event.getY() - startPoint.y);
                        } else if (mode == ZOOM && event.getPointerCount() == 2) {
                            float newDist = spacing(event);
                            matrix.set(savedMatrix);
                            if (newDist > 10f) {
                                float scale = newDist / oldDist;
                                matrix.postScale(scale, scale, midPoint.x,
                                        midPoint.y);
                            }
                            if (lastEvent != null) {
                                newRot = rotation(event);
                                float r = newRot - d;
                                matrix.postRotate(r, middleside.getMeasuredWidth() / 2,
                                        middleside.getMeasuredHeight() / 2);
                            }
                        }
                        break;
                }

                middleside.setImageMatrix(matrix);

                return true;
            }

            private float rotation(MotionEvent event) {
                double delta_x = (event.getX(0) - event.getX(1));
                double delta_y = (event.getY(0) - event.getY(1));
                double radians = Math.atan2(delta_y, delta_x);

                return (float) Math.toDegrees(radians);
            }

            @SuppressLint("FloatMath")
            private float spacing(MotionEvent event) {
                float x = event.getX(0) - event.getX(1);
                float y = event.getY(0) - event.getY(1);
                return (float) Math.sqrt(x * x + y * y);
            }

            private void midPoint(PointF point, MotionEvent event) {
                float x = event.getX(0) + event.getX(1);
                float y = event.getY(0) + event.getY(1);
                point.set(x / 2, y / 2);
            }
        });


        leftside.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                ImageView leftside = (ImageView) v;
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        savedMatrix.set(matrix);
                        startPoint.set(event.getX(), event.getY());
                        mode = DRAG;
                        lastEvent = null;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist = spacing(event);
                        if (oldDist > 10f) {
                            savedMatrix.set(matrix);
                            midPoint(midPoint, event);
                            mode = ZOOM;
                        }
                        lastEvent = new float[4];
                        lastEvent[0] = event.getX(0);
                        lastEvent[1] = event.getX(1);
                        lastEvent[2] = event.getY(0);
                        lastEvent[3] = event.getY(1);
                        d = rotation(event);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (leftClickCount == 0) {

                            left = true;
                            right = false;

                        }
                    case MotionEvent.ACTION_POINTER_UP:

                        mode = NONE;
                        lastEvent = null;
                        break;

                    case MotionEvent.ACTION_MOVE:

                        if (mode == DRAG) {
                            // ...
                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - startPoint.x,
                                    event.getY() - startPoint.y);
                        } else if (mode == ZOOM && event.getPointerCount() == 2) {
                            float newDist = spacing(event);
                            matrix.set(savedMatrix);
                            if (newDist > 10f) {
                                float scale = newDist / oldDist;
                                matrix.postScale(scale, scale, midPoint.x,
                                        midPoint.y);
                            }
                            if (lastEvent != null) {
                                newRot = rotation(event);
                                float r = newRot - d;
                                matrix.postRotate(r, leftside.getMeasuredWidth() / 2,
                                        leftside.getMeasuredHeight() / 2);
                            }
                        }

                        break;
                }

                leftside.setImageMatrix(matrix);

                return true;
            }

            private float rotation(MotionEvent event) {
                double delta_x = (event.getX(0) - event.getX(1));
                double delta_y = (event.getY(0) - event.getY(1));
                double radians = Math.atan2(delta_y, delta_x);

                return (float) Math.toDegrees(radians);
            }

            @SuppressLint("FloatMath")
            private float spacing(MotionEvent event) {
                float x = event.getX(0) - event.getX(1);
                float y = event.getY(0) - event.getY(1);
                return (float) Math.sqrt(x * x + y * y);
            }

            private void midPoint(PointF point, MotionEvent event) {
                float x = event.getX(0) + event.getX(1);
                float y = event.getY(0) + event.getY(1);
                point.set(x / 2, y / 2);
            }
        });


    }








    public Bitmap takeSnap() {
        imgContainer.setDrawingCacheEnabled(true);
        imgContainer.buildDrawingCache();
        Bitmap bitmap = imgContainer.getDrawingCache();
        return bitmap;
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FilePickerConst.REQUEST_CODE:

                if (resultCode == RESULT_OK && data != null) {
                    filePaths = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS);

                    Spacecraft s;
                    try {
                        for (String path : filePaths) {
                            s = new Spacecraft();
                            s.setUri(Uri.fromFile(new File(path)));
                            spacecrafts.add(s);
                        }
                        setImages();
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
        }

    }

    private void setImages() {
        Picasso.with(this).load(spacecrafts.get(0).getUri()).into(leftside);
        Picasso.with(this).load(spacecrafts.get(1).getUri()).into(middleside);
        Picasso.with(this).load(spacecrafts.get(2).getUri()).into(rightside);

    }


    public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {


        Integer[] horizontalList;
        Context context;


        public HorizontalAdapter(Integer[] horizontalList, Activity context) {
            this.horizontalList = horizontalList;
            this.context = context;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {


            holder.imageView.setImageResource(horizontalList[position]);


        }

        @Override
        public int getItemCount() {
            return horizontalList.length;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            ImageView imageView;

            public MyViewHolder(View view) {
                super(view);
                imageView = view.findViewById(R.id.recycle_image);



                        }

          mImage.setImageResource(Utils.mThumbIds[getAdapterPosition()]);


                        takeSnap();



                    }
                });

            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

func insert(book: inout Book) throws {
    let query = "INSERT INTO book (bookName, bookAuthor, bookDesc, bookDate, bookImg, createdBy) VALUES (?, ?, ?, ?, ?, ?)"

    let statement = try database.prepare(query, parameters: [
        book.title, book.author, book.bookDescription, book.createDate, book.image, book.createdBy
    ])
    try statement.step()
    book.id = Int(database.lastRowId())
}

func update(book: Book) throws {
    let query = "UPDATE Book SET bookName = ?, bookAuthor = ?, bookDesc = ?, bookDate = ?, bookImg = ?, createdBy = ?, WHERE bookId = ?"

    let statement = try database.prepare(query, parameters: [
        book.title, book.author, book.bookDescription, book.createDate, book.image, book.createdBy, book.id
    ])
    try statement.step()
}

func delete(book: Book) throws {
    let query = "DELETE FROM Book WHERE bookId = ?"
    let statement = try database.prepare(query, parameters: [book.id])
    try statement.step()
}

func select(bookId: Int) throws -> Book? {
    let query = "SELECT bookId, bookName, bookAuthor, bookDesc, bookDate, bookImg, createdBy FROM Book WHERE bookId = ?"
    let statement = try database.prepare(query, parameters: [bookId])
    if try statement.step() == .row {
        return book(for: statement)
    } else {
        return nil
    }
}

func selectAll() throws -> [Book] {
    let query = "SELECT bookId, bookName, bookAuthor, bookDesc, bookDate, bookImg, createdBy FROM Book"
    let statement = try database.prepare(query)

    var books = [Book]()
    while try statement.step() == .row {
        if let book = book(for: statement) {
            books.append(book)
        }
    }
    return books
}

func book(for statement: Statement) -> Book? {
    guard
        let id = Int(from: statement, index: 0),
        let title = String(from: statement, index: 1),
        let author = String(from: statement, index: 2),
        let description = String(from: statement, index: 3),
        let date = Date(from: statement, index: 4),
        let createdBy = String(from: statement, index: 6) else {
            return nil
    }

    let data = Data(from: statement, index: 5)

    return Book(id: id, image: data, title: title, author: author, bookDescription: description, createDate: date, createdBy: createdBy)
}