最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android實(shí)現(xiàn)簡單圓弧效果

 更新時間:2020年08月17日 12:00:50   作者:紅色與青色  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡單圓弧效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近項(xiàng)目完成就開始搞一些有用沒用的東西,以前面試的時候有人問我那種圓弧效果怎么做,還問我翻牌效果,我只看過,沒有做過,現(xiàn)在有空了,而且想到可能會用到就做個簡單的
圓弧很簡單,自定義個View,創(chuàng)建個Paint,設(shè)置 arcPaint.setStyle(Paint.Style.STROKE)再設(shè)置圓弧的寬,再在onDraw內(nèi)調(diào)用canvas.drawArc()就好了
現(xiàn)在只做一個帶刻度的圓弧和一個開口地方是圓角的圓弧。其他各種效果以后再摸索

ArcView.java

public class ArcView extends View {
  private Paint textPaint;
  private Paint arcPaint;
  private Shader backGradient;
  private Xfermode xfermode;
  private RectF oval = new RectF();
  public ArcView(Context context) {
    super(context);
    init();
  }
 
  public ArcView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  private int type = 0;
 
  public void setType(int type) {
    this.type = type;
    if(type == 1){
      start = 10;
    }
  }
  private void init(){
    arcPaint = new Paint();
    arcPaint.setAntiAlias(true);
    if(type == 0){
      xfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
    }
 
    textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(50);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTextAlign(Paint.Align.CENTER);
 
  }
 
  private int strokeWidth = 40;
 
  public void setStrokeWidth(int strokeWidth) {
    this.strokeWidth = strokeWidth;
  }
 
  private int max = 100;
 
  public void setMax(int max) {
    this.max = max;
  }
 
  private int progress;
 
  public void setProgress(int progress) {
    this.progress = progress;
    postInvalidate();
  }
 
  private int start = 0;
 
  public void setStart(int start) {
    if(type == 1){
      if(start < 10){
        start = 10;
      }
    }else{
      if(start < 0){
        start = 0;
      }
    }
 
    this.start = start;
  }
 
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(getWidth() != 0){
      int width = getWidth();
      int height = getHeight();
      int cx = width/2;
      int cy = height/2;
      if(backGradient == null){
        oval.set( strokeWidth/2, strokeWidth/2,
            width - strokeWidth/2, height - strokeWidth/2);
        int colorStart = getResources().getColor(R.color.colorPrimary);
        int color2 = Color.GREEN;
        int colorEnd = Color.RED;
 
        backGradient = new SweepGradient(cx,cy,new int[]{color2 ,colorStart, colorEnd},new float[]{0.1f,0.4f,0.9f});
 
        postInvalidate();
      }else{
        int sc = 0;
        if(type == 0){
          sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
        }else{
          canvas.save();
        }
 
        canvas.rotate(90,cx,cy);
        arcPaint.setColor(Color.GRAY);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(strokeWidth);
        if(type == 1){
          arcPaint.setStrokeCap(Paint.Cap.ROUND);
        }
 
        int s =start;
        int e = start*2;
        //底色
        canvas.drawArc(oval,s,360 - e,false,arcPaint);
        arcPaint.setShader(backGradient);
        //漸變
        int sweep = (int) (progress*1.0f/max*(360 - e));
        canvas.drawArc(oval,s,sweep,false,arcPaint);
 
        arcPaint.setShader(null);
 
 
        if(type == 0){
          //刻度
          arcPaint.setXfermode(xfermode);
          arcPaint.setStyle(Paint.Style.STROKE);
          arcPaint.setStrokeWidth(5);
 
          for (int i = 0; i < 36;i++){
            canvas.drawLine(0,cy,getWidth(),cy,arcPaint);
            canvas.rotate(5,cx,cy);
          }
          arcPaint.setXfermode(null);
          canvas.restoreToCount(sc);
        }else{
          canvas.restore();
        }
 
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        int baseLineY = (int) (cy - top/2 - bottom/2);
        canvas.drawText(progress+"%",cx,baseLineY,textPaint);
 
        //十字線,用來參考的,可刪除
        canvas.drawLine(cx,0,cx,height,textPaint);
        canvas.drawLine(0,cy,width,cy,textPaint);
 
      }
 
    }
 
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.hyq.hm.testdraw.MainActivity">
 
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
 
    <SeekBar
      android:id="@+id/seek_bar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="20dp"
      android:max="100"/>
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <com.hyq.hm.testdraw.ArcView
        android:id="@+id/arc_view_0"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:background="#885453"
        />
      <com.hyq.hm.testdraw.ArcView
        android:id="@+id/arc_view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        />
    </LinearLayout>
  </LinearLayout>
 
 
</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
 
  private SeekBar seekBar;
  private ArcView arcView0;
  private ArcView arcView1;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    arcView0 = findViewById(R.id.arc_view_0);
    arcView1 = findViewById(R.id.arc_view_1);
    arcView0.setType(0);
    arcView1.setType(1);
    arcView0.setStart(10);
    arcView1.setStart(0);
 
    seekBar = findViewById(R.id.seek_bar);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        arcView0.setProgress(progress);
        arcView1.setProgress(progress);
      }
 
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
 
      }
 
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
 
      }
    });
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中使用Alarm的方法小結(jié)

    Android中使用Alarm的方法小結(jié)

    Alarm是android提供的用于完成鬧鐘式定時任務(wù)的類,系統(tǒng)通過AlarmManager來管理所有的Alarm,下面這篇文章主要給大家介紹了關(guān)于Android中使用Alarm的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05
  • 使用SurfaceView實(shí)現(xiàn)視頻彈幕

    使用SurfaceView實(shí)現(xiàn)視頻彈幕

    這篇文章主要為大家詳細(xì)介紹了使用SurfaceView實(shí)現(xiàn)視頻彈幕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理

    詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理

    這篇文章主要為大家介紹了詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • fragment中的add和replace方法的區(qū)別淺析

    fragment中的add和replace方法的區(qū)別淺析

    使用 FragmentTransaction 的時候,它提供了這樣兩個方法,一個 add , 一個 replace ,對這兩個方法的區(qū)別一直有點(diǎn)疑惑。下面小編通過本文給大家簡單介紹下fragment中的add和replace方法的區(qū)別,一起看看吧
    2017-01-01
  • Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)

    Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)

    這篇文章主要介紹了Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Android Studio 安裝配置方法完整教程【小白秒懂】

    Android Studio 安裝配置方法完整教程【小白秒懂】

    這篇文章主要介紹了Android Studio 安裝配置方法完整教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-03-03
  • Android中的Service相關(guān)全面總結(jié)

    Android中的Service相關(guān)全面總結(jié)

    接下來將介紹Service的種類;Service與Thread的區(qū)別;Service的生命周期;startService 啟動服務(wù);Local與Remote服務(wù)綁定等等,感興趣的朋友可以了解下
    2013-01-01
  • Android實(shí)現(xiàn)圖片浮動隨意拖拽效果

    Android實(shí)現(xiàn)圖片浮動隨意拖拽效果

    這篇文章主要介紹了Android的圖片在界面隨意拖動的功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2018-04-04
  • 淺談Android Studio JNI生成so庫

    淺談Android Studio JNI生成so庫

    下面小編就為大家?guī)硪黄獪\談Android Studio JNI生成so庫。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android TextView實(shí)現(xiàn)詞組高亮的示例代碼

    Android TextView實(shí)現(xiàn)詞組高亮的示例代碼

    本篇文章主要介紹了Android TextView實(shí)現(xiàn)詞組高亮的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論

新乐市| 武清区| 广州市| 酒泉市| 浦城县| 惠水县| 高雄县| 丹东市| 汉沽区| 晋宁县| 富民县| 安溪县| 三门县| 富川| 平谷区| 文昌市| 恩平市| 从江县| 盐城市| 大竹县| 太仆寺旗| 凯里市| 松原市| 绥宁县| 苏州市| 阿拉尔市| 全州县| 遂昌县| 内黄县| 岢岚县| 阿城市| 乐安县| 嵩明县| 高陵县| 光泽县| 蒙城县| 东明县| 正宁县| 湟中县| 仲巴县| 新乐市|