选中后,在水平滚动视图高亮/色调中创建imageView

时间:2017-10-18 12:54:31

标签: android android-layout

screenshot 你好。以上是我的申请。当您在scrollview中选择一个图像时,会有一个onClickListener弹出Toast,告诉您所选的文件。我试图使图像突出显示/着色或类似的东西,以表明图像已被选中。

知道我该如何做到这一点?

下面是我的类,它扩展了scrollview的LinearLayout

package com.example.android.bluetoothlegatt;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MyHorizontalLayout extends LinearLayout {

Context myContext;
ArrayList<String> itemList = new ArrayList<String>();

public MyHorizontalLayout(Context context) {
    super(context);
    myContext = context;
}

public MyHorizontalLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    myContext = context;
}

public MyHorizontalLayout(Context context, AttributeSet attrs,
                          int defStyle) {
    super(context, attrs, defStyle);
    myContext = context;
}

void add(String path){
    int newIdx = itemList.size();
    itemList.add(path);
    addView(getImageView(newIdx));
}

ImageView getImageView(final int i){
    Bitmap bm = null;
    if (i < itemList.size()){
        bm = decodeSampledBitmapFromUri(itemList.get(i), 220, 220);
    }

    ImageView imageView = new ImageView(myContext);
    imageView.setLayoutParams(new LayoutParams(400, 400));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageBitmap(bm);

    imageView.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            DeviceControlActivity.m_Text = itemList.get(i);
            Toast.makeText(myContext,
                    "Clicked - " + DeviceControlActivity.m_Text,
                    Toast.LENGTH_LONG).show();

        }});

    return imageView;
}

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
    Bitmap bm = null;

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public int calculateInSampleSize(

        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }

    return inSampleSize;
}

}

0 个答案:

没有答案