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)
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;
}
If some ImageViews are related to GridView or ListView, and we want to display some images on screen, it often happens the value of an image's width and height are zero(0), so it occurs Exception when we pass this arguments to calculate the compressed Bitmap, I review some frameworks but it's still a problem for me.I want to obtain some kernel solutions to solve this problem. what should I do when the ImageView's width and height is 0.Any help is much appreciated.
public void loadImage(final ImageView imageView, final String imageUrl){
mExecutor.execute(new PriorityRunnable(mThreadsCount) {
#Override
public void doSomething() {
while(!mExecutor.isPause()){
if(imageView != null && imageView.getTag().equals(imageUrl)){
mImageCache.loadImage(imageView, imageUrl);
}
}
}
});
}
public void loadImage(ImageView imageView, String imageUrl){
//readFromMemoryCache
Bitmap bitmap = getBitmap(imageUrl);
if(bitmap != null){
mImageView = imageView;
mBitmap = bitmap;
mHandler.sendEmptyMessage(DISPLAY_IMAGE);
return;
}
int width = imageView.getWidth();
int height = imageView.getHeight();
//because width = 0, so it doesn't do anything, only return
if(width == 0 || height == 0){
return;
}
//load from http or disk
bitmap = getBitmap(imageUrl, width, height);
if(bitmap != null){
mImageView = imageView;
mBitmap = bitmap;
mHandler.sendEmptyMessage(DISPLAY_IMAGE);
}
}
You need to use a global layout listener.
Something like:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (yourView.getHeight() > 0 && yourView.getWidth() > 0) {
CompatUtil.removeOnGlobalLayoutListener(yourView, this);
// do your stuff
}
}
});
I'm really new at this and I'm trying to load profile pics from gallery and camera into ImageButton. I'm able to add image from gallery but the image does not fit completely into the ImageButton. Some space still gets left out from the imagebutton. I want the image to automatically fit into imagebutton.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sam.sport.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LL1" >
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" >
<ImageButton
android:id="#+id/profile_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Activity class is as below:-
public class MainActivity extends Activity {
ImageButton ib;
private static Bitmap Image = null;
private static final int GALLERY = 1;
private static Bitmap rotateImage = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (ImageButton)findViewById(R.id.profile_pic);
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ib.setImageBitmap(null);
if (Image != null)
Image.recycle();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode != 0) {
Uri mImageUri = data.getData();
try {
Image = Media.getBitmap(this.getContentResolver(), mImageUri);
if (getOrientation(getApplicationContext(), mImageUri) != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
if (rotateImage != null)
rotateImage.recycle();
rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix,
true);
ib.setImageBitmap(rotateImage);
} else
ib.setImageBitmap(Image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor.getCount() != 1) {
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
}
Convert it to Drawable then use setBackgroundDrawable insted of setImageBitmap
Drawable drawable = new BitmapDrawable(rotateImage);
ib.setBackgroundDrawable(drawable);
Use below line of code for re-sizing your bitmap
Bitmap BITMAP_IMAGE = Bitmap.createBitmap(IMAGE_VIEW.getWidth(),IMAGE_VIEW.getHeight(), Bitmap.Config.RGB_565);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(BITMAP_IMAGE, 100, 100, false); ////Here BITMAP_IMAGE is your bitmap which you want to resize
Get the imageButton height and width
Bitmap bitmapScaled = Bitmap.createScaledBitmap(Image, ib_width, ib_height, true);
Drawable drawable = new BitmapDrawable(bitmapScaled);
ib.setBackgroundDrawable(drawable);
Keep in mind about OOM exception.
To get imageButton height and width look into this and this
I've got the perfect solution. To resize imageview I used the below code as provided in the [Android ImageView adjusting parent's height and fitting width
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
]1
After searching a lot , i post this thread as a last hope .
i placed two images one above the other and a seek bar over it , i want the upper image to get cropped in such a way that the lower image gets the visibility of the cropped space alone . And the progress is managed through the seekbar thumb .
this is how my view looks like .
and when i move the seekbar , the image gets cropped by rightside but i want the image to get cropped by left .
my class file
skbr = (SeekBar)findViewById(R.id.seekbar);
skbr.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
Bitmap img = getcroppedimg(arg0.getProgress());
img2.setImageBitmap(img);
img2.getLayoutParams().width = img.getWidth();
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
Bitmap img = getcroppedimg(arg0.getProgress());
img2.setImageBitmap(img);
img2.getLayoutParams().width = img.getWidth();
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2)
{
Bitmap img = getcroppedimg(arg0.getProgress());
img2.setImageBitmap(img);
img2.getLayoutParams().width = img.getWidth();
}
});
public Bitmap getcroppedimg(double y)
{
Log.e(" progress ", ""+y);
int x = (int)Math.round(( y / 100)*600);
Bitmap originalImg1 = BitmapFactory.decodeResource(getResources(), R.drawable.neutral_left_without);
Bitmap originalImg = BitmapFactory.decodeResource(getResources(), R.drawable.neutral_left_without);
Bitmap cropimg ;
if((originalImg.getWidth()-x)>0)
cropimg = Bitmap.createBitmap(originalImg, 0, 0, originalImg.getWidth()-x, originalImg.getHeight());
else if((originalImg.getWidth()+x)<= originalImg1.getWidth())
cropimg = Bitmap.createBitmap(originalImg, 0, 0, originalImg.getWidth()+x, originalImg.getHeight());
else
cropimg = Bitmap.createBitmap(originalImg, 0, 0, originalImg.getWidth(), originalImg.getHeight());
return cropimg ;
}
Please let me know where i am going wrong or is there any alternative to this . Any related answers are welcomed .
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.