直接上代碼
1)實現普通效果
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:shadowColor="#ff0000" android:shadowDx="3" android:shadowDy="3" android:shadowRadius="1" android:text="abcdefg" android:textColor="#0000ff" android:textSize="100sp" />
運行結果如下
2)測試下各個屬性值影響
1.
android:shadowRadius="0"
結果
所以,只要讓
shadowRadius = 0,就不會有陰影顯示
2.
android:shadowRadius="20"
結果
它控制的主要就是陰影的寬度,它的值也大,陰影越大,而且顏色越淡
3.測試下dx,dy的作用
android:shadowDx="30"
android:shadowDy="30"
結果
也就是陰影的偏移量。
總結如下
1. android:shadowColor:陰影的顏色
2. android:shadowDx:水平方向上的偏移量
3. android:shadowDy:垂直方向上的偏移量
4. android:shadowRadius:是陰影的的半徑大小
以上變量全是px單位。而且,如果您想用@dimen引用,會報錯。
如果你想在代碼中使用,可以使用如下方法
package com.example.imagetest; import android.R.integer; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { TextView tv; Button bt; int a; float t1; float t2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); bt = (Button) findViewById(R.id.bt); a = 0; t1 = getResources().getDimension(R.dimen.activity_horizontal_margin); t2 = getResources().getDimension(R.dimen.activity_vertical_margin); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (a == 1) { // R.color沒有效果 tv.setShadowLayer(t1, t1, t1, R.color.aaa); a = 0; } else { tv.setShadowLayer(t2, t2, t2, 0x800000ff); a = 1; } bt.setText(a + ""); } }); } }
也就是setShadowLayer方法
setShadowLayer(radius, dx, dy, color);
它的四個參數,分別對應上面的四個屬性
四個屬性取值,要么直接寫,要么使用getResource進行一步轉化才行