Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize fillet and circle picture based on Fresco in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the "Android based on Fresco how to achieve fillet and circle picture" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "Android based on Fresco how to achieve fillet and circle picture" article can help you solve the problem.

Fresco itself has implemented the function of rounded corners and round pictures.

At work, when we encounter a round avatar, UI usually gives us such a picture as the default picture.

In theory, you only need to add the following line of code to complete this part of the work.

App:placeholderImage= "@ drawable/ic_avatar_default"

However, the round picture itself is already round, and this style appears on some models.

Search for a wave, the built-in properties can not solve this problem, simply define the realization of this circle, at the same time Fresco comes with the fillet effect can only ensure the use of a unified radius, want to make the radius of the four fillets different, can only be set in the java file, not flexible, define the properties of the fillet radius also need to make some changes.

Idea: custom RoundImageView inherits from SimpleDraweeVie and has all its functions.

Canvas's clipPath (Path path) can clip the Canvas to the graphics we want according to Path.

Public class RoundImageView extends SimpleDraweeView {private final static int DEFAULT_VALUE = 0; private float mWidth; private float mHeight; private Path mPath; / / fillet angle private float mCornerRadius; / / upper left corner fillet angle private float mLeftTopRadius; / / upper right corner fillet angle private float mRightTopRadius; / / lower right corner fillet angle private float mRightBottomRadius; / / lower left corner fillet angle private float mLeftBottomRadius / / whether to use circular picture private boolean mAsCircle; / / Circular picture radius private float mRadius; public RoundImageView (Context context) {this (context, null);} public RoundImageView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public RoundImageView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); initData () InitAttrs (context, attrs);} private void initData () {mPath = new Path ();} private void initAttrs (Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes (attrs, R.styleable.RoundImageView); mCornerRadius = typedArray.getDimension (R.styleable.RoundImageView_cornerRadius, DEFAULT_VALUE); mAsCircle = typedArray.getBoolean (R.styleable.RoundImageView_asCircle, false) If (mCornerRadius mCornerRadius & & mHeight > mCornerRadius) {/ / set the xpene y radius values of the four corners float [] radius = {mLeftTopRadius, mLeftTopRadius, mRightTopRadius, mRightTopRadius, mRightBottomRadius, mRightBottomRadius, mLeftBottomRadius, mLeftBottomRadius}; mPath.addRoundRect (mWidth, mWidth, mHeight), radius, Path.Direction.CW); canvas.clipPath (mPath);}

The attr attribute is as follows

This is the end of the content about "how to realize rounded corners and round pictures based on Fresco in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report