I know there are many questions but it do not work out for me so i am posting this question:
I a adding a circular Imageview to my application, and i want some elevation/shadow to it so that it worked on all Api not only after 21. so what should i do?
this is my code:
xml :
<?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="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/mybackground"
/>
</RelativeLayout>
this is mybackground in drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>
this is my code :
public class MainActivity extends AppCompatActivity {
Bitmap bmp;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.frames);
bmp = getroundedBitmap(bmp);
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bmp);
//iv.setBackground(new BitmapDrawable(getResources(),bmp));
Outline outline = null;
/* if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
// outline = new Outline();
// outline.setOval(0,0,iv.getWidth(),iv.getHeight());
iv.setElevation(1500);
}*/
}
private Bitmap getroundedBitmap(Bitmap bmp) {
int targetwidth = 250;
int targetheight = 250;
Bitmap targetBitmap = Bitmap.createBitmap(targetwidth,targetheight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetwidth - 1) / 2, ((float) targetheight - 1) / 2, Math.min(targetwidth, targetheight) / 2, Path.Direction.CCW);
canvas.clipPath(path);
Paint p = new Paint();
p.setColor(Color.WHITE);
canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getHeight()), new Rect(0, 0, targetwidth, targetheight), null);
return targetBitmap;
}
}
Please help me out. i ma working in android studio