下面列出了android.view.View#setLayerType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void animateStar(final View view, int delay) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1f);
alpha.setDuration(100);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(alpha, scaleX, scaleY);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.setStartDelay(delay);
animatorSet.setDuration(600);
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
animatorSet.start();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Music.showStar();
}
}, delay);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private ObjectAnimator createBaseWobble(final View v) {
if (!isPreLollipop())
v.setLayerType(LAYER_TYPE_SOFTWARE, null);
ObjectAnimator animator = new ObjectAnimator();
animator.setDuration(180);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setPropertyName("rotation");
animator.setTarget(v);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
v.setLayerType(LAYER_TYPE_NONE, null);
}
});
return animator;
}
@SuppressLint("NewApi")
private void setSepiaLayer(View view, boolean isSepiaLayerOn) {
if (!IS_JBMR2) {
if (isSepiaLayerOn) {
if (Build.VERSION.SDK_INT >= 17) {
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
view.setLayerPaint(mSepiaPaint);
}
} else {
if (Build.VERSION.SDK_INT >= 17) {
view.setLayerPaint(mDefaultPaint);
}
}
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void manageLayer(View v, boolean enableHardware)
{
if (!API_11)
return;
int layerType = enableHardware ? View.LAYER_TYPE_HARDWARE
: View.LAYER_TYPE_NONE;
if (layerType != v.getLayerType())
v.setLayerType(layerType, null);
}
public ShadowCircleTransformation(Context context, View view, float strokeSize) {
super();
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
this.strokeSize = strokeSize;
shadowColor = Color.WHITE;
spaceBetween = .95f;
}
public void onAnimationStart(Animator animation) {
getViews(mViews);
for (View view : mViews) {
mLayerType.add(view.getLayerType());
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
}
/**
* Enable a hardware layer for the view.
*
* @param v
* @param drawWithLayer
*/
private void setDrawWithLayer(View v, boolean drawWithLayer) {
if (isHardwareAccelerated()) {
if (v.getLayerType() != LAYER_TYPE_HARDWARE && drawWithLayer) {
v.setLayerType(LAYER_TYPE_HARDWARE, null);
} else if (v.getLayerType() != LAYER_TYPE_NONE && !drawWithLayer) {
v.setLayerType(LAYER_TYPE_NONE, null);
}
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void disableHardwareLayer()
{
if (!API_11)
return;
View v;
for (int i = 0; i < getChildCount(); i++)
{
v = getChildAt(i);
if (v.getLayerType() != View.LAYER_TYPE_NONE)
v.setLayerType(View.LAYER_TYPE_NONE, null);
}
}
/**
* 为指定View设置带阴影的背景
* @param view 目标View
* @param bgColor View背景色
* @param shapeRadius View的圆角
* @param shadowColor 阴影的颜色
* @param shadowRadius 阴影的宽度
* @param offsetX 阴影水平方向的偏移量
* @param offsetY 阴影垂直方向的偏移量
*/
public static void setShadowDrawable(View view, int bgColor, int shapeRadius, int shadowColor, int shadowRadius, int offsetX, int offsetY) {
ShadowDrawable drawable = new ShadowDrawable.Builder()
.setBgColor(bgColor)
.setShapeRadius(shapeRadius)
.setShadowColor(shadowColor)
.setShadowRadius(shadowRadius)
.setOffsetX(offsetX)
.setOffsetY(offsetY)
.builder();
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
ViewCompat.setBackground(view, drawable);
}
/**
* 为指定View设置指定形状并带阴影的背景
* @param view 目标View
* @param shape View的形状 取值可为:GradientDrawable.RECTANGLE, GradientDrawable.OVAL, GradientDrawable.RING
* @param bgColor View背景色
* @param shapeRadius View的圆角
* @param shadowColor 阴影的颜色
* @param shadowRadius 阴影的宽度
* @param offsetX 阴影水平方向的偏移量
* @param offsetY 阴影垂直方向的偏移量
*/
public static void setShadowDrawable(View view, int shape, int bgColor, int shapeRadius, int shadowColor, int shadowRadius, int offsetX, int offsetY) {
ShadowDrawable drawable = new ShadowDrawable.Builder()
.setShape(shape)
.setBgColor(bgColor)
.setShapeRadius(shapeRadius)
.setShadowColor(shadowColor)
.setShadowRadius(shadowRadius)
.setOffsetX(offsetX)
.setOffsetY(offsetY)
.builder();
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
ViewCompat.setBackground(view, drawable);
}
private void prepareStartAnimation(View v) {
// Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
// callback below)
if (v != null) {
v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void disableHardwareLayer() {
if (!API_11) return;
View v;
for (int i = 0; i < getChildCount(); i++) {
v = getChildAt(i);
if (v.getLayerType() != View.LAYER_TYPE_NONE)
v.setLayerType(View.LAYER_TYPE_NONE, null);
}
}
public static void a(View view, int i)
{
view.setLayerType(i, null);
}
public static void setLayerType(View view, int layerType) {
view.setLayerType(layerType, null);
}
public static void setLayerType(View view, int layerType) {
view.setLayerType(layerType, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Set the View layer to a hardware rendering layer
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
// Set a tag on the View so we can use it for custom animations
view.setTag(this);
// Title
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(getTitle());
title.setTextColor(getTitleColor());
// Description
TextView description = (TextView) view.findViewById(R.id.description);
description.setText(getDescription());
description.setTextColor(getDescriptionColor());
// ViewStub
ViewStub viewStub = (ViewStub) view.findViewById(R.id.viewstub);
switch (getResourceType()) {
// If the resource ID represents a layout ID,
// inflate the layout based on the ID
case RESOURCE_TYPE_LAYOUT:
if (getLayoutId() != 0) {
viewStub.setLayoutResource(getLayoutId());
viewStub.inflate();
}
break;
// If the resource ID represents a drawable ID,
// inflate the drawable layout and set the image resource
case RESOURCE_TYPE_DRAWABLE:
viewStub.setLayoutResource(R.layout.drawable_layout);
viewStub.inflate();
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(getDrawableId());
break;
}
}
public static void setLayerType(View view, int layerType) {
view.setLayerType(layerType, null);
}
public static void setLayerType(View view, int layerType) {
view.setLayerType(layerType, null);
}
public static void setLayerType(View view, int layerType) {
view.setLayerType(layerType, null);
}
public static void setLayerType(View view, int layerType, Paint paint) {
view.setLayerType(layerType, paint);
}