I have a CardView (with multiple items like imageView, textView, Buttons) in a RecyclerView. I want to get bitmap from this cardview. I tried a simple method of converting cardView into bitmap (convert a cardview to bitmap) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver.
CardView extend view, so you probably can convert it as any other view,
try to pass the CardView into that function:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view
Related
I've been trying to draw a Canvas on an ImageView but after creating a bitmap I see no Image on my screen. The only thing I get is the small circle I've drawn in the onDraw Method. I want to have the Canvas on the ImageView.
I've got the following code:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Bitmap myBitmap;
Paint paint;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
}
public void onClickButton(View view) {
View v = new DrawCanvas(getApplicationContext());
Bitmap bitmap = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
imageView = (ImageView) findViewById(R.id.testpicture);
imageView.setImageBitmap(bitmap);
}
}
and
public class DrawCanvas extends View {
Paint paint = new Paint();
public DrawCanvas(Context context) {
super(context);
}
public DrawCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawCircle(25, 25, 5, paint);
}
}
Thanks in advance!
First, you're overcomplicating things. You don't need, or want, a view to do what you're doing. If you want to draw a circle, do it directly to the canvas by using canvas.drawCircle in your onClick.
Second, alling setImageBitmap overwrites any previous bitmap you set. If you want this to show over the previous image, you have 2 options:
1)Use two image views on top of one another, and set the top image to the bitmap made on the canvas (to make this work you may need to make sure the background color on the bitmap is transparent).
2)Create a new view subclassed off ImageView that displays 2 bitmaps by overriding onDraw.
The first approach is probably easier.
I need to convert TextView into ImageView so I will be able to save the ImageView as an image in the sd card.
How do I convert TextView into ImageView?
Thanks.
(I saw the other questions but them are not realy answer my question).
Yes there is a way to do this, by two ways.
You can create an Bitmap of any View using buildDrawingCache() and getDrawingCache()
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
You can also check this answer for further reference.
In the other way , you can take your whole rootview as a screen shot and you can save it into disc.
This will help to achieve the above How to programatically take a screenshot on Android?
public static Bitmap convertViewToBitmap(View view)
{
view.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
I have a gridView with 4 columns of imgs. When phone is turned to landscape layout, grids of the gridView expand to fill the width, creating white spaces in between the img although the img stay at desired width. Is there a way to keep the grid view from filling width of screen when in landscape layout ?
I have 2 methods to do that:
1) Create layout for each orientation:
res/layout/....
res/layout-land/....
2) Only one layout for both vertical and horizontal orientation:
set up GridView with: android:numColumns="4"
your custom view item, always set android:layout_width="match_parent"
with the second method, you may encount problem with high of item. Just create your custom View and override onMeasure.
For example: I need a LinearLayout for my item and the keep my ratio height/width = 4.5/4, so I do the following custom:
public class MyLinearLayout extends LinearLayout {
//..... constructor methods
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int scaleHeight =(int)( widthMeasureSpec/4*4.5);
super.onMeasure(widthMeasureSpec, scaleHeight);
}
}
I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed.
I know I can swap out the ImageView image when the state changes through drawables, but I cannot seem to find the ideal way to affect another view in the layout while isPressed is true on this specific image view.
Ultimately, I am trying to create a bottom ActionBar and simulate the regular ActionBar highlight box (that is, when you press a menu item in the ActionBar you get the highlight box). Right now I have the ImageView a LinearLayout with a small (8dp) padding on the top and bottom. I can replace the image in the ImageView with one that has a 50% white background, but I cannot do it this way if I want to keep the images device density independent. Instead, I'd like to have a square layout the button exists in that I would change the color of as needed.
Ideas?
"I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed."
for this :
you define your ImageView in XML as clickable android:clickable="true"
you affect an OnClickListener to this ImageView in your Activity onCreat() :
ImageView yourImage = (ImageView) findViewById(R.id.your_image);
yourImage.setClickable(true); // if you want to define it here
yourImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.your_linear_layout).setBackgroundColor(your_color);
}
});
Else if you want to change the color only when it is clicked and restore the old color after the click, you can implement OnTouchListener :
yourImage.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
// when the click begins
findViewById(R.id.your_linear_layout).setBackgroundColor(your_click_color);
return true;
} else {
// when the click finishs
findViewById(R.id.your_linear_layout).setBackgroundColor(your_init_color);
return true;
}
return false;
}
});
I hope I helped...
I have an ImageView that displays an image in fullscreen in the background. This is done in this way.
public void handleImage(String imageLocation) {
LinearLayout layout = (LinearLayout) findViewById(R.id.mediaView);
ImageView image = new ImageView(this);
layout.addView(image);
Uri imageLocUri = Uri.parse(imageLocation);
image.setImageURI(imageLocUri);
image.setScaleType(ImageView.ScaleType.FIT_XY);
}
Now I need to do the same for video, but VideoView does not have .setScaleType().
The video needs to be playable on click, and I will be displaying buttons and stuff on top of the video.
Any ideas?