日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一起来学习android自定义控件3——边缘凹凸的View

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一起来学习android自定义控件3——边缘凹凸的View 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

最近做項目的時候遇到一個卡劵的效果,由于自己覺得用圖片來做的話可以會出現適配效果不好,再加上自己自定義view方面的知識比較薄弱,所以想試試用自定義View來實現。先看設計圖效果

?

實現分析

上面的圖片其實和普通的Linearlayout,RelativeLayout一樣,只是上下兩邊多了類似于半圓鋸齒的形狀。那么只需要處理不同地方。可以在上下兩條線上畫一個個白色的小圓來實現這種效果。

假如我們上下線的半圓以及半圓與半圓之間的間距是固定的,那么不同尺寸的屏幕肯定會畫出不同數量的半圓,那么我們只需要根據控件的寬度來獲取能畫的半圓數。

大家觀察圖片,很容易發現,圓的數量總是圓間距數量-1,也就是,假設圓的數量是circleNum,那么圓間距就是circleNum+1。

所以我們可以根據這個計算出circleNum. circleNum = (int) ((w-gap)/(2*radius+gap)); 這里gap就是圓間距,radius是圓半徑,w是view的寬。

看代碼

?

public class CouponDisplayView extends LinearLayout {private Paint mPaint;/*** 圓間距*/private float gap = 8;/*** 半徑*/private float radius = 10;/*** 圓數量*/private int circleNum;private float remain;public CouponDisplayView(Context context) {super(context);}public CouponDisplayView(Context context, AttributeSet attrs) {super(context, attrs);mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setDither(true);mPaint.setColor(Color.WHITE);mPaint.setStyle(Paint.Style.FILL);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);if (remain==0){remain = (int)(w-gap)%(2*radius+gap);}circleNum = (int) ((w-gap)/(2*radius+gap));}public CouponDisplayView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}

?

上面定義了圓的半徑和圓間距,同時初始化了這些值并且獲取了需要畫的圓數量。 接下來只需要一個一個將圓畫出來就可以了。

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);for (int i=0;i<circleNum;i++){float x = gap+radius+remain/2+((gap+radius*2)*i);canvas.drawCircle(x,0,radius,mPaint);canvas.drawCircle(x,getHeight(),radius,mPaint);}}

?

簡單的根據circleNum的數量進行了圓的繪制。

這里remain/2是因為,可以一些情況,計算出來的可以畫的數量不是剛好整除的。這樣就會出現右邊最后一個間距會比其它的間距都要寬。

所以我們在繪制第一個的時候加上了余下的間距的一半,即使是不整除的情況。至少也能保證第一個和最后一個間距寬度一致。

這樣就實現了。 看看效果

<?xml version="1.0" encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="20dp"><com.qiangyu.test.view.CouponDisplayViewandroid:orientation="horizontal" android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/indicator_color"android:padding="20dp"><ImageViewandroid:layout_width="120dp"android:layout_height="match_parent"android:src="@drawable/goods_test"android:scaleType="centerCrop"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:paddingLeft="16dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18dp"android:text="美食劵"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12dp"android:padding="5dp"android:text="編號:11223124123213131"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12dp"android:padding="5dp"android:text="編號:11223124123213131"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12dp"android:paddingLeft="5dp"android:paddingTop="5dp"android:text="截止日期:2001-09-07"/></LinearLayout></com.qiangyu.test.view.CouponDisplayView> </FrameLayout>

?

效果圖

?

覺得不錯,點贊評論一下唄。

?源碼下載

轉載于:https://www.cnblogs.com/yangqiangyu/p/5499945.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的一起来学习android自定义控件3——边缘凹凸的View的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。