亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義View實(shí)現(xiàn)左右滑動(dòng)選擇出生年份

 更新時(shí)間:2016年06月07日 11:13:04   作者:tyktfj0910  
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)左右滑動(dòng)選擇出生年份,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

自定義view的第三篇,模仿的是微博運(yùn)動(dòng)界面的個(gè)人出生日期設(shè)置view,先看看我的效果圖:

支持設(shè)置初始年份,左右滑動(dòng)選擇出生年份,對(duì)應(yīng)的TextView的值也會(huì)改變。這個(gè)動(dòng)畫(huà)效果弄了好久,感覺(jué)還是比較生硬,與微博那個(gè)還是有點(diǎn)區(qū)別。大家有改進(jìn)的方案,歡迎一起交流。

自定義View四部曲,這里依舊是這個(gè)套路,看看怎么實(shí)現(xiàn)的。

1.自定義view的屬性:
在res/values/ 下建立一個(gè)attrs.xml , 在里面定義我們的屬性以及聲明我們的整個(gè)樣式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
 //自定義屬性名,定義公共屬性
 <attr name="titleSize" format="dimension"></attr>
 <attr name="titleText" format="string"></attr>
 <attr name="titleColor" format="color"></attr>
 <attr name="outCircleColor" format="color"></attr>
 <attr name="inCircleColor" format="color"></attr>
 <attr name="lineColor" format="color"></attr>

 <declare-styleable name="MyScrollView">
  <attr name="titleSize"></attr>
  <attr name="titleColor"></attr>
  <attr name="lineColor"></attr>
 </declare-styleable>

</resources>

依次定義了字體大小,字體顏色,線的顏色3個(gè)屬性,format是值該屬性的取值類型。
然后就是在布局文件中申明我們的自定義view:

 <TextView
  android:id="@+id/year_txt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margin="30dp"
  android:text="出生年份 (年)"
  android:textSize="20dp" />

 <com.example.tangyangkai.myview.MyScrollView
  android:id="@+id/scroll_view"
  android:layout_width="match_parent"
  android:layout_height="70dp"
  myscroll:lineColor="@color/font_text"
  myscroll:titleColor="@color/strong"
  myscroll:titleSize="30dp">
 </com.example.tangyangkai.myview.MyScrollView>

自定義view的屬性我們可以自己進(jìn)行設(shè)置,記得最后要引入我們的命名空間,
xmlns:app=”http://schemas.Android.com/apk/res-auto”

2.獲取自定義view的屬性:

 public MyScrollView(Context context) {
  this(context, null);
 }

 public MyScrollView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public MyScrollView(final Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  //獲取我們自定義的樣式屬性
  TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyScrollView, defStyleAttr, 0);
  int n = array.getIndexCount();
  for (int i = 0; i < n; i++) {
   int attr = array.getIndex(i);
   switch (attr) {
    case R.styleable.MyScrollView_lineColor:
     // 默認(rèn)顏色設(shè)置為黑色
     lineColor = array.getColor(attr, Color.BLACK);
     break;
    case R.styleable.MyScrollView_titleColor:
     textColor = array.getColor(attr, Color.BLACK);
     break;
    case R.styleable.MyScrollView_titleSize:
     // 默認(rèn)設(shè)置為16sp,TypeValue也可以把sp轉(zhuǎn)化為px
     textSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
       TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
     break;

   }

  }
  array.recycle();
  init();
 }

 private void init() {
  //初始化
  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mBound = new Rect();
  mTxtBound = new Rect();
  bigTxtSize = textSize;
  oneSize = textSize - 15;
  thirdSize = textSize - 15;
 }

自定義View一般需要實(shí)現(xiàn)一下三個(gè)構(gòu)造方法,這三個(gè)構(gòu)造方法是一層調(diào)用一層的,屬于遞進(jìn)關(guān)系。因此,我們只需要在最后一個(gè)構(gòu)造方法中來(lái)獲得View的屬性以及進(jìn)行一些必要的初始化操作。盡量不要在onDraw的過(guò)程中去實(shí)例化對(duì)象,因?yàn)檫@是一個(gè)頻繁重復(fù)執(zhí)行的過(guò)程,new是需要分配內(nèi)存空間的,如果在一個(gè)頻繁重復(fù)的過(guò)程中去大量地new對(duì)象會(huì)造成內(nèi)存浪費(fèi)的情況。

3.重寫(xiě)onMesure方法確定view大?。?/strong>

上一篇自定義View的文章介紹的很詳細(xì),這里就不重復(fù)了,重點(diǎn)放在onDraw方法里面:
Android自定義View仿微博運(yùn)動(dòng)積分動(dòng)畫(huà)效果

4.重寫(xiě)onDraw方法進(jìn)行繪畫(huà):

之前說(shuō)過(guò)對(duì)于比較復(fù)雜的自定義View,重寫(xiě)onDraw方法之前,首先在草稿本上將大致的樣子畫(huà)出來(lái),坐標(biāo),起始點(diǎn)都可以簡(jiǎn)單標(biāo)注一下。這個(gè)方法很實(shí)用,思路很清晰。

A點(diǎn)的位置就是繪制數(shù)字的初始位置,B點(diǎn)的位置就是繪制豎線的起始位置。確定好這兩個(gè)初始位置,我們只要寫(xiě)一個(gè)循環(huán),找到規(guī)律,依次繪制出后面的線與字即可。因?yàn)樯婕白笥一瑒?dòng)的事件處理,所以需要Android事件分發(fā)的知識(shí)來(lái)進(jìn)行處理。

 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {

  int action = ev.getAction();
  int x = (int) ev.getX();
  int y = (int) ev.getY();
  switch (action) {
   case MotionEvent.ACTION_DOWN:
    xDown = x;
    yDown = y;
    break;
   case MotionEvent.ACTION_MOVE:
    xMove = x;
    yMove = y;
    dx = xMove - xDown;
    int dy = yMove - yDown;
    //如果是從左向右滑動(dòng)
    if (xMove > xDown && Math.abs(dx) > mTouchSlop * 2 && Math.abs(dy) < mTouchSlop) {
     state = 1;
    }
    //如果是從右向左滑動(dòng)
    if (xMove < xDown && Math.abs(dx) > mTouchSlop * 2 && Math.abs(dy) < mTouchSlop) {
     state = 2;

    }
    break;
   case MotionEvent.ACTION_UP:
    break;
  }
  return super.dispatchTouchEvent(ev);
 }

重寫(xiě)View的dispatchTouchEvent方法來(lái)區(qū)別左右滑動(dòng),mTouchSlop是Android默認(rèn)的滑動(dòng)最小距離,如果水平方向滑動(dòng)的距離大于豎直方向滑動(dòng)的距離,就判斷為水平滑動(dòng)。這里為了不讓滑動(dòng)那么明顯,我讓水平滑動(dòng)的距離大于默認(rèn)距離的兩倍才判定左右滑動(dòng)。state是記錄滑動(dòng)的狀態(tài)。

 

 @Override
 public boolean onTouchEvent(MotionEvent ev) {

  int action = ev.getAction();
  switch (action) {
   case MotionEvent.ACTION_DOWN:
    break;
   case MotionEvent.ACTION_MOVE:
    if (state == 1 && bigTxtSize - oneSize > -15) {
     bigTxtSize = bigTxtSize - 1;
     oneSize = oneSize + 1;
     postInvalidate();
    }
    if (state == 2 && bigTxtSize - thirdSize > -15) {
     bigTxtSize = bigTxtSize - 1;
     thirdSize = thirdSize + 1;
     postInvalidate();
    }
    break;
   case MotionEvent.ACTION_UP:
    if (state == 1) {
     size = size - 1;
     bigTxtSize = textSize;
     oneSize = textSize - 15;
     postInvalidate();
     listener.OnScroll(size);
     state = 0;
    }
    if (state == 2) {
     size = size + 1;
     bigTxtSize = textSize;
     thirdSize = textSize - 15;
     postInvalidate();
     listener.OnScroll(size);
     state = 0;
    }
    break;
  }
  return true;
 }

重寫(xiě)View的onTouchEvent方法來(lái)處理View的點(diǎn)擊事件。
(1)演示動(dòng)態(tài)圖中,左右滑動(dòng)的過(guò)程中,中間數(shù)字會(huì)從大變小,左右的數(shù)字會(huì)從小變大,bigTxtSize代表中間的數(shù)字大小,oneSize代表從左到右第二個(gè)數(shù)字的大小,thirdSize代表從左到右第四個(gè)數(shù)字的大小。在滑動(dòng)過(guò)程中再使用postInvalidate()方法來(lái)一直調(diào)用onDraw方法來(lái)重新進(jìn)行繪制,達(dá)到數(shù)字大小變化的效果。
(2)滑動(dòng)結(jié)束以后進(jìn)行判斷,如果是從左向右滑動(dòng),就會(huì)將數(shù)字減一;如果是從右向左滑動(dòng),就會(huì)將數(shù)字加一。最后將數(shù)字大小,滑動(dòng)狀態(tài)恢復(fù)到默認(rèn)值。
(3)最后一定要返回true,表示消費(fèi)當(dāng)前滑動(dòng)事件,不然滑動(dòng)沒(méi)反應(yīng)

滑動(dòng)的操作已經(jīng)全部處理好,接下來(lái)就是繪制:

 @Override
 protected void onDraw(Canvas canvas) {
  txtSize = size - 2;
  bigText = String.valueOf(size);
  smallText = String.valueOf(txtSize);
  mPaint.setColor(lineColor);
  canvas.drawLine(0, 0, getWidth(), 0, mPaint);
  canvas.drawLine(0, getHeight(), getWidth(), getHeight(), mPaint);
  lineX = getWidth() / 10;
  for (int i = 0; i < 5; i++) {
   if (i == 2) {
    mPaint.setTextSize(bigTxtSize);
    if (bigTxtSize == textSize - 15) {
     mPaint.setColor(lineColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 5, mPaint);
    } else {
     mPaint.setColor(textColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 3, mPaint);
    }
    mPaint.getTextBounds(bigText, 0, bigText.length(), mBound);
    canvas.drawText(bigText, lineX - mBound.width() / 2, getHeight() / 2 + mBound.height() * 3 / 4, mPaint);
   } else if (i == 0 || i == 4) {
    mPaint.setColor(lineColor);
    mPaint.setTextSize(textSize - 15);
    mPaint.getTextBounds(smallText, 0, smallText.length(), mTxtBound);
    canvas.drawLine(lineX, 0, lineX, getHeight() / 5, mPaint);
    canvas.drawText(String.valueOf(txtSize), lineX - mTxtBound.width() / 2, getHeight() / 2 + mTxtBound.height() * 3 / 4, mPaint);
   } else if (i == 1) {
    mPaint.setTextSize(oneSize);
    if (oneSize == textSize) {
     mPaint.setColor(textColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 3, mPaint);
    } else {
     mPaint.setColor(lineColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 5, mPaint);
    }
    mPaint.getTextBounds(smallText, 0, smallText.length(), mTxtBound);
    canvas.drawText(String.valueOf(txtSize), lineX - mTxtBound.width() / 2, getHeight() / 2 + mTxtBound.height() * 3 / 4, mPaint);
   } else {
    mPaint.setTextSize(thirdSize);
    if (thirdSize == textSize) {
     mPaint.setColor(textColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 3, mPaint);

    } else {
     mPaint.setColor(lineColor);
     canvas.drawLine(lineX, 0, lineX, getHeight() / 5, mPaint);
    }
    mPaint.getTextBounds(smallText, 0, smallText.length(), mTxtBound);
    canvas.drawText(String.valueOf(txtSize), lineX - mTxtBound.width() / 2, getHeight() / 2 + mTxtBound.height() * 3 / 4, mPaint);
   }
   txtSize++;
   lineX += getWidth() / 5;
  }

 }

這里其實(shí)就是得到滑動(dòng)操作的數(shù)字尺寸大小,然后進(jìn)行繪制,最后將數(shù)字每次加一,lineX是B點(diǎn)的初始位置,每次加上寬度的五分之一。

5.得到當(dāng)前的設(shè)置值
可以看到View上面的TextView也會(huì)跟著下面設(shè)置的值改變,所以這里我們需要單獨(dú)處理一下。接口回調(diào),簡(jiǎn)單暴力的方式。

在onTouchEvent的case MotionEvent.ACTION_UP中,得到最后設(shè)置的值

  listener.OnScroll(size);

然后就是對(duì)應(yīng)的Activity了:

public class SecondActivity extends AppCompatActivity implements MyScrollView.OnScrollListener {

 private MyScrollView scrollView;
 private TextView txt;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_second);
  initview();
 }

 private void initview() {
  scrollView = (MyScrollView) findViewById(R.id.scroll_view);
  scrollView.setSize(1992);
  scrollView.setListener(this);
  txt = (TextView) findViewById(R.id.year_txt);
  txt.setText("出生年份" + scrollView.getSize() + " (年)");
 }

 @Override
 public void OnScroll(int size) {
  txt.setText("出生年份" + size + " (年)");
 }
}

實(shí)現(xiàn)接口的方法,進(jìn)行初始化,設(shè)置初始值,然后就是在接口的方法更新數(shù)據(jù)即可。

自定義view的第一篇:Android自定義View實(shí)現(xiàn)BMI指數(shù)條

自定義view的第二篇:Android自定義View仿微博運(yùn)動(dòng)積分動(dòng)畫(huà)效果

OK,下一篇自定義View再見(jiàn)。

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

相關(guān)文章

最新評(píng)論