Hello is there a way to grayscale image background? I achieved grayscaling image itself, but not the background :/.
My GridView getView:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(65, 65));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(13, 13, 13, 13);
} else {
imageView = (ImageView) convertView;
}
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setImageResource(MainActivity.mThumbIds.get(position));
imageView.setBackgroundResource(R.drawable.check);
imageView.setColorFilter(filter);
return imageView;
}
Not directly, but you can render the resource to a bitmap, apply the filter to the bitmap, and set the background to the filtered bitmap using a BitmapDrawable and imageView.setBackground(Drawable).
Related
If I have (1)(2)(3)(4)(5)(6)(7)(8) images in GridView show - > (1)(2)(3)(4)(1)(2)(3)(4) which this code and this work which very good performance.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {//here
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
File imgFile = new File(ImagesApi.getCalculatedList().get(
position));
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
imageView.setImageBitmap(Bitmap.createScaledBitmap(myBitmap,
(int) (myBitmap.getWidth() * 0.12),
(int) (myBitmap.getHeight() * 0.12), true));
myBitmap.recycle();
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
public int getCount() {
return ImagesApi.getCalculatedList().size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
If I add if (convertView == null || position != convertView.getId()) { and imageView.setId(position); in "//here" I get images in GridView which correct order but which very very bad performance. Ui thread go very very slow.
ImagesApi.getCalculatedList() is constant ArrayList of image paths
I'm using an adapter view for for a list view to retrieve " Publications " from Parse.com, I'm using an animation for loading image from Parse. and i'm using another function to get a circle view of the image.
all is working great, the loading is perfect but when I slide down/up on the list view the image is animated again each time I slide Up/Down. and I couldn't stop that.
here is the code of my adapter :
public class adapterview extends ArrayAdapter<Message> {
Bitmap image;
public adapterview(Context context, ArrayList<Message> Messages) {
super(context, 0, Messages);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final Message m = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom2, parent, false);
}
TextView message = (TextView) convertView.findViewById(R.id.message);
TextView date = (TextView) convertView.findViewById(R.id.date);
TextView user = (TextView) convertView.findViewById(R.id.user);
message.setText(m.getMessage());
user.setText(m.getUser());
new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageView1))
.execute(m.getImage());
return convertView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
TransitionDrawable td = new TransitionDrawable(new Drawable[]{
new ColorDrawable(android.R.color.transparent),
new BitmapDrawable(getContext().getResources(), getCircleBitmap(result))
});
bmImage.setImageDrawable(td);
td.startTransition(2000);
}
}
private Bitmap getCircleBitmap(Bitmap bitmap) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
if you need any other information you think could be helpful for you to determine the problem just let me know
When a list needs to display a item it calls its Adapters getView() function. When you scroll and list items are no longer visible they are release and not kept in memory.
When scrolling every list item has DownloadImageTask() executed each time it becomes visible. The downloaded image from function is stored only in the ImageView of list item, so is it released from memory as soon as you scroll the list item.
The solution is to store the downloaded image then in getView() function assign the image into the ImageView.
i try to make my image view position dynamic base on image height. but i stuck to get the image height from drawable.. help me to solve it thanks..
this is my code :
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_wall, null, true);
TextView txtProfileUser = (TextView) rowView.findViewById(R.id.txtWallProfile);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtWallContentTitle);
TextView txtContent = (TextView) rowView.findViewById(R.id.txtWallContent);
ImageView imgWallProfile = (ImageView) rowView.findViewById(R.id.ivWallProfile);
ImageView imgWallContent = (ImageView) rowView.findViewById(R.id.ivWallContent);
txtTitle.setText(web2[position]);
txtContent.setText(web3[position]);
txtProfileUser.setText(web[position]);
imgWallProfile.setImageResource(imageId[position]);
imgWallContent.setImageResource(imageId2[position]);
GetSize gz = new GetSize();
Integer h = gz.getImageSize(imageId[position]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20,h, 0, 0);
imgWallProfile.setLayoutParams(lp);
return rowView;
}
and this my getImageSize method :
public Integer getImageSize(Integer d){
Drawable bd = getResources().getDrawable(d);
int height=bd.getIntrinsicHeight();
return height;}
If I understand correctly you want to get the width and height of your image view instead of the bitmaps width and height. Layout parameters allows you to access the imageView and view and modify it's parameters.
public int getImageSize(View myView) {
RelativeLayout.LayoutParams myParams = (RelativeLayout.LayoutParams) myView.getLayoutParams();
return myParams.height;
}
I have the following GridView:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setScaleType(ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
if (mTemplates.get(position).getResource() != 0) {
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from resource" + mTemplates.get(position).getResource() + "???????????" + mTemplates.size());
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
// imageView.setImageResource(mTemplates.get(0).getResource());
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from drawable");
LogService.log("", "=========SIZE: " + mTemplates.size());
}
return imageView;
}
Now I have some elements that do not have resources in the Drawables, but instead in a folder on the sdcard, so I create a drawable from the source of this pictures, and try to load that drawable on the gridview:
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
But its like the gridview does not recognise these, and if its on the same collumn with other pictures it is shown, but if its on another collumn (lower), it will only show 2-3mm of the picture (also doesn't recognise its position like its not therE).
If I hardcode and use the first picture from Resources like this:
imageView.setImageResource(mTemplates.get(0).getResource());
It all works, what could be the issue?
Because the actually size of the pic, is the small size, but when loading an imageView from Resources it strectches it to its parents size.
Resolved this issue by calculating the height needed, and then load all the pictures with that height:
public AdapterGridView(Context c) {
context = c;
DisplayMetrics metrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (Constants.IS_TABLET) {
height = metrics.widthPixels / 9 - (int) Util.pxFromDp(2, c);
} else {
height = metrics.widthPixels / 4 - (int) Util.pxFromDp(2, c);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
if (mTemplates.get(position).getResource() != 0) {
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
}
imageView.setLayoutParams(new LayoutParams(height, height));
return imageView;
}
I'm trying to surround the top and bottom of my ImageView in a Gallery with a layout containing a tiled background. The top part seems to be working, but the bottom layout doesn't show. Any ideas why? Here's my getView():
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
RelativeLayout topReel = new RelativeLayout(galleryContext);
RelativeLayout bottomReel = new RelativeLayout(galleryContext);
topReel.setBackgroundResource(R.drawable.film_top);
bottomReel.setBackgroundResource(R.drawable.film_bottom);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), imageSizeInPixels);
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(galleryBackground);
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
topParams.addRule(RelativeLayout.ABOVE, imageView.getId());
topParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
bottomParams.addRule(RelativeLayout.BELOW, imageView.getId());
bottomParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.BELOW, topReel.getId());
imageParams.addRule(RelativeLayout.ABOVE, bottomReel.getId());
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.addView(topReel, topParams);
container.addView(bottomReel, bottomParams);
container.addView(imageView, imageParams);
return container;
}
This solves the problem, hopefully someone can use this:
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), ((int)(imageSizeInPixels * .75)));
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundColor(Color.BLACK);
RelativeLayout borderImg = new RelativeLayout(galleryContext);
borderImg.setPadding(10, 5,10, 5);
borderImg.setBackgroundColor(0xff000000);
borderImg.addView(imageView);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.setBackgroundResource(R.drawable.repeat_reel);
container.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels+40));
container.addView(borderImg, imageParams);
return container;
}