当前位置:首页 » 安卓系统 » android圆角阴影

android圆角阴影

发布时间: 2023-01-02 17:02:02

⑴ Android 5.0 elevation 设置阴影无效

elevation 设置阴影需要注意两点:
1、view需要设置背景
2、阴影是绘制在父控件上的,子控件和父控件之间要留空隙,用padding也不行
在开发过程中有过这样的布局:

我子Linearlayout是设置的圆角背景,结果这样的效果是:只有圆角的便便才有一丁点的阴影,原因是父布局设置的是padding,子控件是match_parent,空隙不足导致,将子布局改为margin,去掉父控件的padding就行了

⑵ android 安卓 类似ONE卡片页(包含图片文字等)怎么实现

Android5.0中向我们介绍了一个全新的控件–CardView,从本质上看,可以将CardView看做是FrameLayout在自身之上添加了圆角和阴影效果。请注意:CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。

下面例子来源于android学习手册, android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。360手机助手中下载,排在第四个。

CardView应该被使用在显示层次性的内容时;在显示列表或网格时更应该被选择,因为这些边缘可以使得用户更容易去区分这些内容。

使用CardView

首先,假设你的布局如同下面的形式:

<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Main Content View --> <RelativeLayout> ... </RelativeLayout> </FrameLayout>

为了使用上面的布局方式来创建一个卡片,首先你需要导入支持的依赖库(android-support-v7-cardview的jar包)在你的build.gradle文件中。

dependencies { ... compile 'com.android.support:cardview-v7:21.0.2' }

现在将FrameLayout替换为CardView,

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Main Content View --> <RelativeLayout> ... </RelativeLayout> </android.support.v7.widget.CardView>

就是这样!使用依赖库能够保证你的程序稳定的兼容之前的版本;尽管在AndroidL和之前的Android版本中对其处理方式有所不同。

⑶ 如何在圆形 imageView android 上添加一个阴影和边界

我创建了 CircularImageView 这一问题:创建在 android 中的圆形图像视图
下载GitHub的项目
1) 这是 CircularImageView 类:
public class CircularImageView extends ImageView {
public CircularImageView(Context context) {
super(context);
}

public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}

if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.(Bitmap.Config.ARGB_8888, true);

Bitmap roundBitmap = getCroppedBitmap(bitmap, getWidth());
canvas.drawBitmap(roundBitmap, 0, 0, null);
}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;

Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888);
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setColor(Color.parseColor("#BAB399"));

Canvas c = new Canvas(output);
c.drawARGB(0, 0, 0, 0);
c.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
c.drawBitmap(sbmp, rect, rect, paint);

return output;
}
}

2) 我在我像这样的布局中使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >

<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/imageViewCircular"
android:layout_width="@dimen/image_view_size"
android:layout_height="@dimen/image_view_size"
android:layout_gravity="center"
android:background="@drawable/border"
android:src="@drawable/image" />

</LinearLayout>

⑷ Android设置图片圆角的方法

Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片

通过第三方框架Glide实现图片显示有圆角,有三种写法如下:

1.1、第一种实现:

1.2、第二种实现:

1.3、第三种实现:

自定义ImageView:

对图片进行处理,此方法还可以加边框

实现圆形和边框:

以上就是本文的全部内容,希望对大家的学习有所帮助!

⑸ 同时设置圆角和阴影

先说一下ImageView吧,开始的时候因为我没有拿到UI切图,所以先给它设置了一个背景颜色,然后设置了圆角和阴影。

结果发现圆角有了,阴影却并没有出来。。。

于是发现,是因为layer的masksToBounds设置成true导致把它范围之外的部分给裁剪掉了,所以阴影也被干掉了,于是我把masksToBounds的设置去掉了(默认为false)

结果发现圆角和阴影就都有了

然而事情并没有结束,因为当UI切图出来之后,我设置了一张图片,结果发现圆角又没有了。。。

在这个imageView的下边添加一个底部视图,位置约束跟imageView相同,用来专门设置阴影效果,然后把imageView添加到底部视图上,最后给imageView设置圆角(这次要把masksToBounds设置成true)

圆角和阴影终于出来了:

同理,按钮也是一样的情况,如果只给按钮设置了backgroundColor,并不会有按压效果,如果要有按压效果,就要给button设置BackgroundImage及相应的UIControlState
但是设置之后,效果就如同imageView上述情况一样了,同样解决方法也用以上方法就能搞定。

除了以上这个方法添加view之外,好像也可以添加layer来实现,但是因为layer无法设置约束,还要到viewDidLayoutSubviews方法中设置frame,所以就放弃了。

⑹ Android 实现圆角布局,变相实现圆角图片效果(不同位置不同弧度)

    小菜最近在处理图片的圆角,不止是四个角全是圆角,还包括单左侧/单右侧/对角线方向的圆角。因为自己太菜只能寻求网上的大神,发现一个自定义圆角布局,这样可以变相的解决我的需求,还可以实现更多的圆角效果,不仅是图片,还包括其他布局。
    小菜我作为伸手党,非常感谢大神的分享,参考原文 RoundAngleFrameLayout 。
    这个布局实现方式很简单,大神只提供了默认的四个圆角,这里我添加了一些方法可以动态的设置圆角的位置与弧度,并说明一下小菜遇到的小问题。小菜根据大神的总结自定义了一个 MyRoundLayout GitHub 布局样式。

Tips:

⑺ android 如何重写imageview 让图片有圆角效果

android 自定义圆角ImageView以及锯齿的处理

看到很多人开发过程中要使用圆角图片时,解决方法有:


1.重新绘制一张图片


2.通过布局来配置


3.通过重写View来实现


其中1,2在这里就不讲了,重点讲讲方法三的实现。



实现一:通过截取画布一个圆形区域与图片的相交部分进行绘制,缺点:锯齿明显,设置Paint,Canvas抗锯齿无效。

package com.open.circleimageview.widget;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PaintFlagsDrawFilter;

import android.graphics.Path;

import android.graphics.Rect;

import android.graphics.Region;

import android.util.AttributeSet;

import android.view.View;


public class CircleImageViewA extends View {


public CircleImageViewA(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}


public CircleImageViewA(Context context, AttributeSet attrs) {

super(context, attrs);

}


public CircleImageViewA(Context context) {

super(context);

}


private Bitmap bitmap;

private Rect bitmapRect=new Rect();

private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

private Paint paint = new Paint();

{

paint.setStyle(Paint.Style.STROKE);

paint.setFlags(Paint.ANTI_ALIAS_FLAG);

paint.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了

}

private Path mPath=new Path();

public void setImageBitmap(Bitmap bitmap)

{

this.bitmap=bitmap;

}

@Override

protected void onDraw(Canvas canvas) {


if(null==bitmap)

{

return;

}

bitmapRect.set(0, 0, getWidth(), getHeight());

canvas.save();

canvas.setDrawFilter(pdf);

mPath.reset();

canvas.clipPath(mPath); // makes the clip empty

mPath.addCircle(getWidth()/2, getWidth()/2, getHeight()/2, Path.Direction.CCW);

canvas.clipPath(mPath, Region.Op.REPLACE);

canvas.drawBitmap(bitmap, null, bitmapRect, paint);

canvas.restore();

}

}


实现二:通过PorterDuffXfermode 方式(注意要设置硬件加速,否则部分机子无效),优点:锯齿基本没有

package com.open.circleimageview.widget;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PaintFlagsDrawFilter;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.View;


public class CircleImageViewB extends View {


public CircleImageViewB(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}


public CircleImageViewB(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}


public CircleImageViewB(Context context) {

super(context);

init();

}


private Bitmap bitmap;

private Rect bitmapRect=new Rect();

private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

private Paint paint = new Paint();

{

paint.setStyle(Paint.Style.STROKE);

paint.setFlags(Paint.ANTI_ALIAS_FLAG);

paint.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了

}

private Bitmap mDstB=null;

private PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);

private void init()

{

try {

if(android.os.Build.VERSION.SDK_INT>=11)

{

setLayerType(LAYER_TYPE_SOFTWARE, null);

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void setImageBitmap(Bitmap bitmap)

{

this.bitmap=bitmap;

}


private Bitmap makeDst(int w, int h)

{

Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(bm);

Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

p.setColor(Color.parseColor("#ffffffff"));

c.drawOval(new RectF(0, 0, w, h), p);

return bm;

}

@Override

protected void onDraw(Canvas canvas) {


if(null==bitmap)

{

return;

}

if(null==mDstB)

{

mDstB=makeDst(getWidth(), getHeight());

}


bitmapRect.set(0, 0, getWidth(), getHeight());

canvas.save();

canvas.setDrawFilter(pdf);

canvas.drawBitmap(mDstB, 0, 0, paint);

paint.setXfermode(xfermode);

canvas.drawBitmap(bitmap, null, bitmapRect, paint);

paint.setXfermode(null);

canvas.restore();

}

}


⑻ Android圆角背景设置

使用databinding设置圆角背景,代替drawable方式

注意:这个只是设置一个背景,所有图片的圆角不能使用它,只能是viewGroup或者TextView。
提示:图片可以使用QMUIRadiusImageView

1、支持view和viewGroup的圆角,边框、和单个圆角等;
2、app:bgRadius:圆角大小,必须用"@{R.dimen.ui_dp8}"赋值;
3、app:bgSolidColor:设置背景色;
4、app:bgStrokeColor:设置边框颜色;
5、bgTopLeftRadius:设置左上的圆角;

⑼ Android如何纯java代码实现字体阴影效果

Android实现纯java代码字体阴影效果,主要是通过activity的draw方法,进行重新绘制,如下代码:

packagecanvas.test;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuff.Mode;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.graphics.RectF;
importandroid.graphics.drawable.Drawable;
importandroid.view.View;
{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(newImageEffect(this));
}
classImageEffectextendsView{
Paintpaint;
publicImageEffect(Contextcontext){
super(context);
paint=newPaint();//初始化画笔,为后面阴影效果使用。
paint.setAntiAlias(true);//去除锯齿。
paint.setShadowLayer(5f,5.0f,5.0f,Color.BLACK);//设置阴影层,这是关键。
paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
}
@Override
publicvoidonDraw(Canvascanvas){
super.onDraw(canvas);
intposX=20;
intposY=50;
intPicWidth,PicHegiht;
Drawabledrawable=getResources().getDrawable(R.drawable.button);
Drawabledbe=getResources().getDrawable(R.drawable.button).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。
DrawabledrawTest=getResources().getDrawable(R.drawable.button);
Bitmapbmp=BitmapFactory.decodeResource(getResources(),R.drawable.button);
PicWidth=drawable.getIntrinsicWidth();
PicHegiht=drawable.getIntrinsicHeight();
drawTest.setBounds(posX,(2*posY)+PicHegiht,posX+PicWidth,(2*posY)+2*PicHegiht);
drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHegiht);
dbe.setBounds(0,0,PicWidth,PicHegiht);
canvas.drawColor(Color.WHITE);//设置画布颜色
canvas.save(Canvas.MATRIX_SAVE_FLAG);
dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);
canvas.translate(posX+(int)(0.9*PicWidth/2),posY+PicHegiht/2);//图像平移为了刚好在原图后形成影子效果。
canvas.skew(-0.9F,0.0F);//图像倾斜效果。
canvas.scale(1.0f,0.5f);//图像(其实是画布)缩放,Y方向缩小为1/2。
dbe.draw(canvas);//此处为画原图像影子效果图,比原图先画,则会在下层。
drawable.clearColorFilter();
canvas.restore();
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawable.draw(canvas);//此处为画原图像,由于canvas有层次效果,因此会盖在影子之上。
canvas.restore();
//默认无效果原图
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawTest.draw(canvas);
canvas.restore();
//图片阴影效果
canvas.save(Canvas.MATRIX_SAVE_FLAG);
//Rectrect=newRect(2*posX+PicWidth,2*posY+PicHegiht,2*posX+2*PicWidth,2*posY+2*PicHegiht);//此为理论上的阴影图坐标
Rectrect=newRect(2*posX+PicWidth+3,2*posY+PicHegiht+3,2*posX+2*PicWidth-2,2*posY+2*PicHegiht-2);
//由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果
RectFrectF=newRectF(rect);
canvas.drawRoundRect(rectF,10f,10f,paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。
canvas.drawBitmap(bmp,2*posX+PicWidth,2*posY+PicHegiht,null);//画上原图。
canvas.restore();
}
}
}

⑽ Android中CardView的使用

一、简介

Material  design 中有一种很个性的设计概念:卡片式设计(Cards),这种设计与传统的List Item 有所区别,Cards 包含更多的内容元素和拥有自己的 UI 特征,关于 Cards 的设计规范可以参考官网介绍:

https://material.google.com/components/cards.html#

为了更好地实现这种 Cards  UI  的设计,Google在v7  包中引进了一种全新的控件:CardVew。用一句话介绍了 CardView:一个带圆角和阴影背景的 FrameLayout。CardView在Android Lollipop(API 21)及以上版本的系统中适配较好。

二、导入依赖

compile 'com.android.support:cardview-v7:版本'

三、常用属性

1. API 5 以上(Android 2.0)

1.1 app:cardConerRadius: 圆角的半径

1.2 app:cardBackgroundColor:CardView 的卡片颜色

2. API 21 以上(Android 5.0)

2.1 app:cardElevation:CardView 的 Z 轴阴影

2.2 app:cardMaxElevation:阴影的最大高度

2.3 android:foreground="?android:attr/selectableItemBackground":点击时有波纹发散效果

四、排版技巧

CardView 从本质上属于 FrameLayout,而 CardView 通常包含了较多的内容元素,为了方 便 地 排 版 布 局 中 的 各 个 元 素 , 一 般 借 助 于 其 他 基 本 布 局 容 器 , 比 如 使 用 了 一个RelativeLayout 作为 CardView 的唯一子布局。

热点内容
诛仙3需要什么配置 发布:2025-04-23 00:29:49 浏览:664
什么是编译错误参数不可选 发布:2025-04-23 00:23:06 浏览:520
libx264编译 发布:2025-04-23 00:13:37 浏览:222
access的web数据库 发布:2025-04-23 00:08:29 浏览:46
安卓上面的谷歌搜索框怎么去除 发布:2025-04-23 00:07:27 浏览:171
c判断文件夹是否存在 发布:2025-04-22 23:56:36 浏览:942
精确测算法 发布:2025-04-22 23:54:09 浏览:287
安卓如何更换开机画面 发布:2025-04-22 23:49:10 浏览:889
知道ID密码怎么定位 发布:2025-04-22 23:34:16 浏览:253
c语言采样 发布:2025-04-22 23:30:03 浏览:917