I am getting bitmap of gridview using this line of code
aftershu = Bitmap.createBitmap(image_grid.getDrawingCache());
But when i get this i'm not getting the current view of gridview i'm getting one view before the current view .
Here is my code
image_grid.setDrawingCacheEnabled(true);
image_grid.buildDrawingCache(true);
// Bitmap b = Bitmap.createBitmap(mGridView.getDrawingCache());
beforeshu= Bitmap.createBitmap(image_grid.getDrawingCache());
Bitmap x=getResizedBitmap(beforeshu,200,200);
b1.setImageBitmap(x);
Collections.shuffle(smallimages);
image_grid.setAdapter(new SmallImageAdapter(this, smallimages));
// image_grid.setAdapter(new SmallImageAdapter());
image_grid.setNumColumns((int) Math.sqrt(smallimages.size()));
image_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int firstclick;
int counter=0;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
counter++;
if (counter % 2 == 0) {
firstclick = position;
Bitmap data1 = smallimages.get(position);
} else {
Bitmap swapImage = smallimages.get(position);
smallimages.set(position, smallimages.get(firstclick));
smallimages.set(firstclick, swapImage);
image_grid.invalidateViews();
}
aftershu = Bitmap.createBitmap(image_grid.getDrawingCache());
}
});
this beforeshu is getting correct bitmap of before shuffling but aftershu bitmap is returning the image view before last swap in grid view
I inserted this line after the onclick i.e when clicking the checkresult but it returns always the beforeshu bitmap.
my output left image is before shuffle right image is the bitmap i get when i click checkresult
So how to get the bitmap of current view of gridview i.e.when i click checkresult button
I solved by this method from stackoverflow
public static Bitmap getBitmapFromView(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return bitmap;
}
i passed gridview to this method and got the current bitmap
Related
I want to implement a drag-and-drop functionality within a recyclerview. Everything goes perfectly until I want to customize the looks of the view being dragged (not the view from which the drag event starts, I want to modify the "shadow" and keep the original view the same).
I've tried to make a bitmap out of the view being passed, but the end result is both the original item and the Shadow are modified AND the original view loses its position on the list... WTF
Here is my code:
public class ImageDragShadowBuilder extends View.DragShadowBuilder {
private Bitmap shadow;
LinearLayout linearLayout;
private ImageDragShadowBuilder() {
super();
}
public static ImageDragShadowBuilder create(Context context, View view) {
ImageDragShadowBuilder builder = new ImageDragShadowBuilder();
builder.linearLayout = (LinearLayout) view.findViewById(R.id.metric_item);
builder.linearLayout.setBackgroundResource(R.drawable.background_item_dragging);
builder.shadow = createBitmapFromView(builder.linearLayout);
return builder;
}
public View getLayout() {
return linearLayout;
}
private static Bitmap createBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(new Canvas(b));
return b;
}
#Override
public void onDrawShadow(Canvas canvas) {
canvas.drawBitmap(shadow, 0, 0, null);
}
#Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
shadowSize.x = shadow.getWidth();
shadowSize.y = shadow.getHeight();
shadowTouchPoint.x = shadowSize.x / 2;
shadowTouchPoint.y = shadowSize.y / 2;
}
}
Any ideas?
I think that there are 2 problems :
Setting the background on the view attached to the RecyclerView affects the original item and the shadow
Triggering a layout changes the item's children position
Basically you can use the original view, but you should never modify it. A slightly modified version of your shadow builder could be :
public static ImageDragShadowBuilder create(Context context, View view) {
ImageDragShadowBuilder builder = new ImageDragShadowBuilder();
builder.linearLayout = (LinearLayout) view.findViewById(R.id.metric_item);
// do not change the original view
// we will draw the background directly later
// builder.linearLayout.setBackgroundResource(R.drawable.background_item_dragging);
builder.shadow = createBitmapFromView(builder.linearLayout);
return builder;
}
private static Bitmap createBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
// do not change the original view
// v.layout(0, 0, v.getWidth(), v.getHeight());
Canvas c = new Canvas(b);
// draw the background
Drawable background = v.getContext().getDrawable(R.drawable.background_item_dragging);
background.setBounds(0, 0, b.getWidth(), b.getHeight());
background.draw(c);
v.draw(c);
return b;
}
I want to take screenshot of an activity and show it in next activity on button click.
I'm using following code but its not working.
Please let me know if anyone knows the answer:
choose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mainlayout.setDrawingCacheEnabled(true);
mainlayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(), mainlayout.getMeasuredHeight());
mainlayout.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(mainlayout.getDrawingCache());
mainlayout.setDrawingCacheEnabled(false); // clear drawing cache
Intent i4=new Intent(Screen3.this,Screen4.class);
i4.putExtra("BitmapImage", b);
startActivity(i4);
}//onclick
});
public static Bitmap captureScreen(View v){
Bitmap bitmap = null;
try {
if(v!=null)
{
int width = v.getWidth();
int height = v.getHeight();
Bitmap screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
v.draw(new Canvas(screenshot));
}
}
catch (Exception e){
Log.d("captureScreen", "Failed");
}
return bitmap;
}
v is the what is being capturing (any layout)
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.
Ok so first of I will take you through the basics of my App. So far you take picture using the camera which then gets saved into a folder. Once the picture has been took a new Activity opens and shows the picture on screen in a ImageView.
The effect I am trying to achive is something along the lines of being able to create png layers over the top of the picture in the ImageView with the onClick Event. So say I have 5 transparent png's I want to have five onClick events which with each touch of the screen a new png image will get displayed over the top of the picture in the ImageView so it would take 5 onClick (touch of screen) to show all the transparent png's over the top of the picture.
I think I am nearly there with the code below but I think I need to create an array of the images in the drawable folder that need to be displayed with each onClick event, I think the array should be nextBitmap but not to sure.
Update Sparkys answer
I have tried adding private Integer[] nextBitmap = { R.drawable.img1, R.drawable.img2 }; but I am getting the error The method getBitmapOverlay(Bitmap, Bitmap, int, int) in the type BeatEmUp is not applicable for the arguments (Bitmap, Integer[], int, int) anyone know what this could be do I need to add the private Integer[]...... outside the onCreate method?
public class Image extends Activity {
Bitmap myBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Image);
String myRef = this.getIntent().getStringExtra("filepath");
File imgFile = new File(myRef);
Log.e("No ref", myRef);
if(imgFile.exists()) {
final Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
final ImageView myImage = (ImageView) findViewById(R.id.beatemup);
myImage.setImageBitmap(myBitmap);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myBitmap = getBitmapOverlay(myBitmap, nextBitmap, 0, 0);
myImage.setImageBitmap(myBitmap);
}
});
}
}
public static Bitmap getBitmapOverlay(Bitmap bmp1, Bitmap bmp2, int left, int top) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, left, top, null);
return bmOverlay;
}
}
Ok, I have made you a small working sample that might be what you are looking for.
I break my own rules here because I did code for you. As I know that you are a beginner, you should definitively understand the following code before you use it.
public class Image extends Activity {
Bitmap myBitmap;
Integer[] mBitmapIds = new Integer[] { R.drawable.ic_launcher, R.drawable.ic_launcher };
Random mRand = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
final ImageView myImage = (ImageView) findViewById(R.id.imageview);
myImage.setImageBitmap(myBitmap);
myImage.setOnClickListener(new OnClickListener() {
int i = 0;
public void onClick(View v) {
if (i >= mBitmapIds.length) {
i = 0;
}
myImage.setImageBitmap(getBitmapOverlay(myBitmap, BitmapFactory.decodeResource(getResources(), mBitmapIds[i]), 0, 0));
}
});
}
public Bitmap getBitmapOverlay(Bitmap bmp1, Bitmap bmp2, int left, int top) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(),
bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
left += mRand.nextInt(20);
top += mRand.nextInt(20);
canvas.drawBitmap(bmp2, left, top, null);
return bmOverlay;
}
}
See the Hello, Views tutorial, GridView chapter for an example of an array of references to Drawables.
I have set up a gallery for my app using BaseAdapter. Here is the code I used for the gallery.
homeGallery = (Gallery) findViewById(R.id.homeimggallery);
homeGallery.setSpacing(0);
homeGallery.setSelection(0);
homeGallery.setAdapter(new AddImgAdp(this));
private class AddImgAdp extends BaseAdapter {
private int GalItemBg, temp;
private Context cont;
private View convertView1;
private ViewGroup parent1;
private Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mask);
private Bitmap whiteBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.stroke);
private Bitmap blueBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.strokeactive);
private Bitmap src;
private Bitmap dest;
private Canvas canvas;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ImageView homeImgView;
private ImageView[] homeImgViewArr= new ImageView[arrThumbImage.length];
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
public int getCount() {
return arrThumbImage.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
homeImgView = new ImageView(cont);
try{
src = BitmapFactory.decodeResource(mContext.getResources(), arrThumbImage[position]);
dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(dest);
canvas.drawColor(Color.TRANSPARENT);
canvas.save();
canvas.translate((canvas.getWidth() - src.getWidth())>> 1, (canvas.getHeight() - src.getHeight())>> 1);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(mask, 0, 0, paint);
canvas.drawBitmap(dest, 0, 0, paint);
homeImgView.setBackgroundDrawable(new BitmapDrawable(dest));
homeImgView.setImageResource(R.drawable.stroke);
homeImgView.setScaleType(ImageView.ScaleType.FIT_XY);
homeImgViewArr[position] = homeImgView;
} catch(Exception e) {}
return homeImgView;
}
}
The gallery looks like below:
On finger movement, it is moving right to left or left to right as expected. Now I want to add some onClick action to items. If user click on any image, it will be selected and align in the middle. The following code is used for this action.
homeGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
homeGallery.setSelection(position);
}
});
But it results wrongly. If I am selecting item no. 2, the item no. 3 got selected, although setSelection action is firing against item no 2. If I click on the right most item of the above pic, it is resulting line below:
What is the problem in my code?
I'm working with a Gallery myself, but I'm not quite sure what the problem is here, since your code is further than where I stand.
Anyways, since you click item 3?? and you get item 4 centered I can think of a few options:
-Something is wrong with the array indexes, might wanna take a log of the positions.
-Personally, I work with an array of Integer and on the getView() I just get the position which is easier than what you're doing I believe.
public ArrayList mImageIds = new ArrayList(); the array...
i.setImageResource(mImageIds.get(position)); and in getView() it's sorted.
Hope this is helpful