如何通过单击按钮将多个图像加载到同一图像视图上?

时间:2018-12-28 09:47:13

标签: android

基本上,我想拥有一个AlertDialog,其中多个图像将加载到同一ImageView上。通过单击NEXT按钮,它将从bitmap列表中获得下一个图像bitmap,最后将其加载到该ImageView上。 PREV按钮的大小写相同。它将使用先前的bitmap并将图像加载到相同的ImageView上。

但是问题是在加载第一个图像之后它没有加载下一个图像。如果我单击“下一步”按钮,则它将接下一个bitmap,但 imageview.setImageBitmap(bitmap) 不起作用。

如何删除或清除上一张图片以放置下一张照片?

XML文件如下:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/question"
            android:gravity="center"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp"
            android:text="@string/question"
            android:textSize="14sp"
            android:textColor="@color/light_black"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"/>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="0dp"
            app:cardElevation="0dp">

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:id="@+id/selected_place_images"
                    android:scaleType="centerCrop"/>

            </LinearLayout>

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

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="47dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/previous"
                android:text="@string/previous_page"
                android:textColor="@color/black"
                android:layout_centerVertical="true"
                android:layout_marginStart="10dp"
                android:layout_alignParentStart="true"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:text="@string/next_page"
                android:textColor="@color/black"
                android:layout_centerVertical="true"
                android:layout_marginEnd="10dp"
                android:layout_alignParentEnd="true"/>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

java代码如下:

private void showDialogOfImages() {
    Log.d(TAG,"showDialogOfImages : showing places images");

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    LayoutInflater inflater = this.getLayoutInflater();
    View customLayout = inflater.inflate(R.layout.custom_displaying_selected_place_images,null);

    mSelectedPlaceImages = (ImageView) customLayout.findViewById(R.id.selected_place_images);
    nextPage = (TextView) customLayout.findViewById(R.id.next);
    previousPage = (TextView) customLayout.findViewById(R.id.previous);

    displayPhoto();

    nextPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentPhotoIndex++;
            displayPhoto();
        }
    });
    previousPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentPhotoIndex--;
            displayPhoto();
        }
    });

    builder.setView(customLayout);
    builder.show();
}

private void displayPhoto() {
    if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
        Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
        Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
        mSelectedPlaceImages.setImageBitmap(bitmap);
        setButtonVisibility();
    }
}

private void setButtonVisibility() {
    if(mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() == 1){
        nextPage.setEnabled(false);
        nextPage.setClickable(false);
        nextPage.setTextColor(getResources().getColor(R.color.divider));
        previousPage.setEnabled(false);
        previousPage.setClickable(false);
        previousPage.setTextColor(getResources().getColor(R.color.divider));
    }
    else if (mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() > 1){
        nextPage.setEnabled(true);
        nextPage.setClickable(true);
        nextPage.setTextColor(getResources().getColor(R.color.black));
        previousPage.setEnabled(false);
        previousPage.setClickable(false);
        previousPage.setTextColor(getResources().getColor(R.color.divider));
    }
    else if (mCurrentPhotoIndex == mSelectedLocationPhotosBitmap.size()-1 && mSelectedLocationPhotosBitmap.size() > 1){
        nextPage.setEnabled(false);
        nextPage.setClickable(false);
        nextPage.setTextColor(getResources().getColor(R.color.divider));
        previousPage.setEnabled(true);
        previousPage.setClickable(true);
        previousPage.setTextColor(getResources().getColor(R.color.black));
    }
    else{
        nextPage.setEnabled(true);
        nextPage.setClickable(true);
        nextPage.setTextColor(getResources().getColor(R.color.black));
        previousPage.setEnabled(true);
        previousPage.setClickable(true);
        previousPage.setTextColor(getResources().getColor(R.color.black));
    }
}

2 个答案:

答案 0 :(得分:0)

您需要从imageview中清除上一张图像,然后将新图像放在顶部。

执行以下操作

private void displayPhoto() {
    if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
        Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
        Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
        mSelectedPlaceImages.setImageBitmap(null)
        mSelectedPlaceImages.setImageBitmap(bitmap);
        setButtonVisibility();
    }
}

答案 1 :(得分:0)

尝试一下:

sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT , raw : false }).then(results => {
  console.log(results)
})