欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android高亮引導(dǎo)控件的實現(xiàn)代碼

 更新時間:2018年07月11日 09:08:30   作者:Wang_Yi  
這篇文章主要介紹了Android高亮引導(dǎo)控件的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近項目需求是實現(xiàn)高亮功能引導(dǎo)頁的效果,查了很多資料Android確實沒有類似iOS的摳圖的現(xiàn)成控件,就自己寫一個,具體如下:

Demo

代碼

public class HighLightLayout extends FrameLayout {
  private Paint mPaint;
  private Path mPath = new Path();
  private List<RectRegion> mRegions;

  public HighLightLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(0xAA000000);

    setWillNotDraw(false);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    mPath.reset();
    mPath.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CCW);
    for (RectRegion region : mRegions) {
      RectF rectF = region.rectF;
      if (region instanceof RoundRectRegion) {
        RoundRectRegion roundRectRegion = (RoundRectRegion) region;
        mPath.addRoundRect(rectF, roundRectRegion.rx, roundRectRegion.ry,               Path.Direction.CW);
      } else if (region instanceof CircleRegion) {
        CircleRegion circleRegion = (CircleRegion) region;
        float cX = (rectF.right + rectF.left) / 2;
        float cY = (rectF.bottom + rectF.top) / 2;
        mPath.addCircle(cX, cY, circleRegion.radius, Path.Direction.CW);
      } else if (region instanceof OvalRegion) {
        mPath.addOval(rectF, Path.Direction.CW);
      } else {
        mPath.addRect(rectF, Path.Direction.CW);
      }
    }
    canvas.drawPath(mPath, mPaint);
  }

  public void setRegion(@NonNull RectRegion region) {
    if (mRegions == null) {
      mRegions = new ArrayList<>();
    } else {
      mRegions.clear();
    }
    mRegions.add(region);
    invalidate();
  }

  public void setRegions(@NonNull List<RectRegion> regions) {
    mRegions = regions;
    invalidate();
  }

  @Override
  public void setBackgroundColor(int color) {
    mPaint.setColor(color);
  }
}

HighLightLayout繼承自FrameLayout,重寫了 onDraw 方法來實現(xiàn)高亮區(qū)域的繪制; setRegion 設(shè)置一個高亮區(qū)域, setRegions 設(shè)置多個高亮區(qū)域;重寫 setBackgroundColor 來實現(xiàn)設(shè)置高亮背景色。

Region表示了一個高亮矩形區(qū)域,支持4種高亮類型,

RectRegion 矩形高亮區(qū)域

public class RectRegion implements Parcelable {
  public RectF rectF;
  //... Parcelable實現(xiàn)代碼
}

RoundRectRegion 圓角矩形高亮區(qū)域

public class RoundRectRegion extends RectRegion {
  public float rx, ry;
  //... Parcelable實現(xiàn)代碼
}

CircleRegion 圓形高亮區(qū)域

public class CircleRegion extends RectRegion {
  public float radius;
  //... Parcelable實現(xiàn)代碼
}

OvalRegion 橢圓高亮區(qū)域

public class OvalRegion extends RectRegion {
  //... Parcelable實現(xiàn)代碼
}

使用

創(chuàng)建一個GuideActivity,該Activity根布局是一個HighLightLayout,可以在HighLightLayout中添加任何控件

<wangyi.blog.app.view.HighLightLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/highLightLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".GuideActivity">
</wangyi.blog.app.view.HighLightLayout>

啟動GuideActivity 并傳遞需要高亮顯示的區(qū)域

ArrayList<RectRegion> regions = new ArrayList<>();

    //矩形高亮
    RectF rectF1 = LocationUtils.getViewLocation(mButton1);
    RectRegion region1 = new RectRegion(rectF1);
    regions.add(region1);
    //圓角矩形高亮
    RectF rectF2 = LocationUtils.getViewLocation(mButton2);
    RoundRectRegion region2 = new RoundRectRegion(rectF2, 10, 10);
    regions.add(region2);
    //圓形高亮
    RectF rectF3 = LocationUtils.getViewLocation(mButton3);
    float radius = (rectF3.right - rectF3.left) / 2 + 20;
    CircleRegion region3 = new CircleRegion(rectF3, radius);
    regions.add(region3);
    //橢圓高亮
    RectF rectF4 = LocationUtils.getViewLocation(mButton4);
    LocationUtils.expandRectF(rectF4, 40);
    OvalRegion region4 = new OvalRegion(rectF4);
    regions.add(region4);

    Intent intent = new Intent(this, GuideActivity.class);
    intent.putExtra(GuideActivity.EXTRA_REGION_LIST, regions);
    startActivity(intent);

GuideActivity的onCreate中設(shè)置高亮區(qū)域

ArrayList<RectRegion> regions = getIntent().getParcelableArrayListExtra(EXTRA_REGION_LIST);
    HighLightLayout highLightLayout = findViewById(R.id.highLightLayout);
    highLightLayout.setRegions(regions);

Github

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

相關(guān)文章

  • Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信詳解

    Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信詳解

    這篇文章主要給大家介紹了關(guān)于Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • 避免 Android中Context引起的內(nèi)存泄露

    避免 Android中Context引起的內(nèi)存泄露

    本文主要介紹Android中Context引起的內(nèi)存泄露的問題,這里對Context的知識做了詳細(xì)講解,說明如何避免內(nèi)存泄漏的問題,有興趣的小伙伴可以參考下
    2016-08-08
  • Android 中 Activity顯示隱式跳轉(zhuǎn)

    Android 中 Activity顯示隱式跳轉(zhuǎn)

    這篇文章主要介紹了Android 中 Activity顯示隱式跳轉(zhuǎn)的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Kotlin 編程三分鐘入門

    Kotlin 編程三分鐘入門

    一個多月以來Kotlin從入門到現(xiàn)在,堅持用來開發(fā)的切身感受。因為語法與Java的區(qū)別挺大的一開始很想放棄,如果不是因為項目在使用,想必很少人會嘗試這樣一門小眾語言,但是習(xí)慣后會發(fā)現(xiàn)這些年究竟浪費(fèi)多少時間在寫無用的Java代碼了
    2017-05-05
  • JNI方法實現(xiàn)圖片壓縮(壓縮率極高)

    JNI方法實現(xiàn)圖片壓縮(壓縮率極高)

    這篇文章主要給大家介紹了一種JNI方法實現(xiàn)圖片壓縮(壓縮率極高)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • ObjectAnimator屬性動畫源碼分析篇

    ObjectAnimator屬性動畫源碼分析篇

    今天小編就為大家分享一篇關(guān)于ObjectAnimator屬性動畫源碼分析篇,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Android 中StringBuffer 和StringBuilder常用方法

    Android 中StringBuffer 和StringBuilder常用方法

    這篇文章主要介紹了Android 中StringBuffer 和StringBuilder的常用方法及區(qū)別介紹,需要的朋友可以參考下
    2017-02-02
  • android開發(fā)教程之a(chǎn)ndroid的handler使用方法

    android開發(fā)教程之a(chǎn)ndroid的handler使用方法

    這篇文章主要介紹了android的handler使用方法,大家參考使用吧
    2014-01-01
  • Android架構(gòu)組件Room的使用詳解

    Android架構(gòu)組件Room的使用詳解

    Room其實就是一個orm,抽象了SQLite的使用。這篇文章給大家介紹了Android架構(gòu)組件Room的使用詳解,需要的朋友參考下吧
    2017-12-12
  • Android編程之DatePicker和TimePicke簡單時間監(jiān)聽用法分析

    Android編程之DatePicker和TimePicke簡單時間監(jiān)聽用法分析

    這篇文章主要介紹了Android編程之DatePicker和TimePicke簡單時間監(jiān)聽用法,結(jié)合具體實例形式分析了時間控件DatePicker和TimePicke布局與具體功能實現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02

最新評論