溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實現(xiàn)圖片添加陰影效果

發(fā)布時間:2020-07-18 13:44:58 來源:億速云 閱讀:537 作者:小豬 欄目:移動開發(fā)

這篇文章主要為大家展示了Android如何實現(xiàn)圖片添加陰影效果,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

給圖片添加陰影效果,這是很常見的需求。第一種方法是自定義drawable,使用layer-list定義兩個圖片,代碼如下:

show_view.xml:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 陰影圖片,android:left表示陰影圖片左邊到背景圖片左邊的距離
 android:top表示陰影圖片上邊到背景圖片上邊的距離-->
 <item android:left="5dp"
  android:top="5dp">
  <shape>
   <corners android:radius="25dp"/>
   <solid android:color="#60000000"/>
  </shape>
 </item>
 <!-- 背景圖片,android:right表示陰影圖片右邊到背景圖片右邊的距離
 android:bottom表示陰影圖片下邊到背景圖片下邊的距離-->
 <item android:bottom="5dp"
  android:right="5dp">
  <shape>
   <corners android:radius="25dp"/>
   <solid android:color="#000000"/>
  </shape>
 </item>
</layer-list>

在main.xml中定義一個textview作為待顯示控件,將show_view.xml設(shè)為這個testview的背景,main.xml的代碼如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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.example.liusiyutaloner.frescotest.MainActivity">

 <TextView
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:background="@drawable/shadow_view"/>
</RelativeLayout>

運行程序顯示效果如下:

Android如何實現(xiàn)圖片添加陰影效果

看著還可以,但是這里面有一個缺陷,大家細(xì)看就會發(fā)現(xiàn)這個陰影是實邊的,沒有虛化的效果,這樣就不夠真實,影響用戶體驗。下面我們來看第二種方法。

第二種方式就是自定義view,代碼里通過setShadowLayer繪制圖片陰影,代碼如下:

CustomShadowView類:

package com.example.liusiyutaloner.frescotest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CustomShadowView extends View {
  private Paint mPaint;

  public CustomShadowView(Context context, AttributeSet attrs) {
   super(context, attrs);
   mPaint = new Paint();
   mPaint.setColor(Color.BLACK);
   this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  }

  @Override
  protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   //繪制陰影,param1:模糊半徑;param2:x軸大?。簆aram3:y軸大小;param4:陰影顏色
   mPaint.setShadowLayer(10F, 15F, 15F, Color.GRAY);
   RectF rect = new RectF(0 , 0, 200, 200);
   canvas.drawRoundRect(rect, (float)75, (float)75, mPaint);
  }

}

再將CustomShadowView類加到main.xml中,代碼如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="150dp"
 android:layout_height="150dp"
 tools:context="com.example.liusiyutaloner.frescotest.MainActivity">

 <com.example.liusiyutaloner.frescotest.CustomShadowView
  android:layout_gravity="center"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:layout_centerHorizontal="true" />
</RelativeLayout>

運行即可看到以下效果:

Android如何實現(xiàn)圖片添加陰影效果

可以看到這種方法繪制出的陰影有虛化效果,多了立體感和層次感,所以更推薦使用。

以上就是關(guān)于Android如何實現(xiàn)圖片添加陰影效果的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI