I'm trying to draw a cardview onto a pdf its canvas. Unfortunately the cardview is not drawn. However some test text is drawn. Why is my carview not drawn?
Code:
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas c = page.getCanvas();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1000, 50);
CardView card = new CardView(this);
card.setLayoutParams(params);
card.setRadius(4f);
Bitmap cardBitmap = Utils.loadBitmapFromView(card);
c.drawText("tessssssttt", 30, 30, p);
c.drawBitmap(cardBitmap, new Rect(0,0,1000,50), new Rect(5, 50, 995, 100), p);
document.finishPage(page);
View to bitmap:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
The following code draws CardView to bitmap.
But it's shadow dose not drawn.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getLayoutInflater().inflate(R.layout.card_view, null, false);
Bitmap bitmap = loadBitmapFromView(view, 500, 500);
//This is sample code. So I print the bitmap to ImageView, not to PDF.
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
v.setMinimumWidth(width);
v.setMinimumHeight(height);
v.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
);
v.layout(0, 0, width, height);
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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=".MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
card_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#33000000">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="This is sample."/>
</android.support.v7.widget.CardView>
</FrameLayout>
Related
I am using the below code for taking screenshot in my application.
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap bitmap = rootView.getDrawingCache();
But I am getting an image with two black portion in the top and bottom by the notification and navigation bars. I need to remove those black areas. I need not to replace those area with the notification and navigation bars. But i need the toolbar.
take the drawing cache of the root Layout as below:
//onCreate
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.activity_main);
layout.setDrawingCacheEnabled(true);
And to get bitmap, the usual way,
Bitmap bitmap = layout.getDrawingCache();
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary" />
<!--other content-->
</RelativeLayout>
Use Below Method:
public static Bitmap loadBitmapFromView(Context context, View v) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
v.draw(c);
return returnedBitmap;
}
Pass your parent view inside the argument.
Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen:
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.
Following is the XML layout file where I have added a button, textview and an imageview in a linear layout. And I have also added onClick attribute in the button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_screenshoat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ncrypted.myapplication.Screenshoat">
<Button
android:id="#+id/btnScreenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Taake screen shot"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
</LinearLayout>
This is android activity file where I have added some java code to take screenshot and import android.graphics.Bitmap;, java.io.ByteArrayOutputStream;, java.io.File; etc.
public class Screenshoat extends AppCompatActivity {
Button btnScreenshot;
Bitmap mbitmap;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screenshoat);
btnScreenshot = (Button) findViewById(R.id.btnScreenshot);
imageView = (ImageView) findViewById(R.id.imageView);
btnScreenshot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mbitmap = getBitmapOFRootView(v);
imageView.setImageBitmap(mbitmap);
createImage(mbitmap);
Toast.makeText(Screenshoat.this, "Screen shoat created", Toast.LENGTH_SHORT).show();
}
});
}
public Bitmap getBitmapOFRootView(View v) {
View rootview = v.getRootView();
rootview.setDrawingCacheEnabled(true);
Bitmap bitmap1 = rootview.getDrawingCache();
return bitmap1;
}
public void createImage(Bitmap bmp) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File file = new File(Environment.getExternalStorageDirectory() +
"/capturedscreenandroid.jpg");
try {
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(bytes.toByteArray());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Add Permission to read and write external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I'm trying to draw a simple circle and use it as a background to an ImageView the problem is the circle is drawn with different color whenever I open the application, here is my code.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="16dp"
android:height="16dp"
/>
<solid android:color="#FFF6621F"/>
<ImageView
android:id="#+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shape_circle"/>
You can use following code....
activity_main.xml
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/imgview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView imgview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgview = (ImageView) findViewById(R.id.imgview);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_blogger);
imgview.setImageBitmap(getCircleBitmap(bm));
}
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;
}
}
You can use getCircleBitmap() method for make ImageView in circle shape.
I’ve made a demo for film-strip in ,android. I am binding Images to my LinearLayout runtime, Now i want to save whole, Scrollable View to a bitmap, I have tried a lot but all gives solution to only save current visible screen to bitmap,can anyone please help me how to save whole scrollable View(Which is not visible to screen) in a bitmap, my code is as below:
xml
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:scrollbars="none" >
<ScrollView
android:id="#+id/scroler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</HorizontalScrollView>
java
View insertPhoto(int i) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(252, 252));
layout.setGravity(Gravity.FILL);
final ImageView imageView = new ImageView(getApplicationContext());
imageView1 = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(250, 250));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader.displayImage("file://" + dataT.get(i).sdcardPath,
imageView, new SimpleImageLoadingListener() {
public void onLoadingStarted(String imageUri, View view) {
imageView.setImageResource(R.drawable.no_media);
super.onLoadingStarted(imageUri, view);
}
});
layout.addView(imageView);
return layout;
}
using this I’ve made film-strip in vertical orientation. And now I want bitmap of this film-strip for save it.
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = null;
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
saveImageToInternalStorage(b);
return b;
}
i used bitmap to crop image. now I could draw text on the image, but i want it below the image of same imageview. I have seen some using the textview below the image, but my requirement is to put text and image in single imageview.
my main class:
public class MainActivity extends Activity {
static ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView1);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images_1);
imageView.setImageBitmap(getCircleBitmap(bitmap));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static Bitmap getCircleBitmap(Bitmap bitmap) {
//crop to circle
Bitmap output;
//check if its a rectangular image
if (bitmap.getWidth() > bitmap.getHeight()) {
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
} else {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
}
Canvas canvas = new Canvas(output);
float r = 0;
if (bitmap.getWidth() > bitmap.getHeight()) {
r = bitmap.getHeight() / 2;
} else {
r = bitmap.getWidth() / 2;
}
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
Rect bounds = new Rect();
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;
canvas.drawText("hii",x,y, paint);
return output;
}
in my xml file:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="26dp"
android:layout_marginTop="52dp"
android:src="#drawable/image" />
any help would be appreciated. Thank you
A safe way is to create your image and add it's text under it (in photoshop for example) and then save it as png and use it. it won't use much space and you can have your own font style etc.
however the previous answers are good and you can use them as well
you can try like below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Since a RealativeLayout stacks its children, defining the TextView after ImageView puts it 'over' the ImageView.
Just one suggestion.
Rather adding text to image view add image to text view.
If you can go through few following attributes of textview,i guess you might find some way out.
android:drawableTop=""
android:drawableBottom=""
android:drawableLeft=""
android:drawableRight=""
android:drawableStart=""
android:drawableEnd=""
android:drawablePadding=""
Take one TextView in XML with Image View and access both in java file and set it like that
TextView.setText(yourtext);
imageview.setImageBitmap(yourbitmapobject);
I am making an app in which i am opening a camera and above the camera i am drawing a flower picture from a custom view canvas object. Now i want to take picture what should i do to take picture of camera with that flower.
I have used this layout for that
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layoutCam">
<FrameLayout
android:id="#+id/preview"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
<ImageView
android:id="#+id/imgHole"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Take Snap"
android:id="#+id/btnTakeSnap"/>
</FrameLayout>
and java code is
public void onClick(View v)
{
preview.camera.takePicture(shutterCallback, rawCallback, postViewCallback, jpegCallback);
chal();
}
private boolean chal()
{
buttonTakeSnap.setVisibility(View.INVISIBLE);
View v1 = camView.getChildAt(1);
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
bmp = v1.getDrawingCache();
v1.setDrawingCacheEnabled(true);
buttonTakeSnap.setVisibility(View.VISIBLE);
Toast.makeText(this, "Akhtitaam", Toast.LENGTH_SHORT).show();
return true;
}
using this code i got two bitmaps one from chal(); method and second from jpegCallBack(); method now you can put these bitmaps on a same layout to overlap and capture that view
Using this code
Use this method to capture views
public static Bitmap loadBitmapFromView(View v)
{
Paint drawPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
drawPaint.setAntiAlias(false);
drawPaint.setFilterBitmap(false);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(b, 0, 0, drawPaint);
v.draw(c);
return b;
}
pass your camera's surface view as a parameter like this
View view=(YourParentLayout) findViewById(R.id.YourParentLayout);
loadBitmapFromView(view);