中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android 中viewpager+fragment仿微信底部TAG完美漸變

發布時間:2020-10-13 02:40:38 來源:腳本之家 閱讀:209 作者:ganchuanpu 欄目:移動開發

viewpager+fragment仿微信底部TAG完美漸變,在圖片漸變的同時字的顏色也在變,注意,是漸變哦!

效果圖:

android 中viewpager+fragment仿微信底部TAG完美漸變

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:zhy="http://schemas.android.com/apk/res/com.Sing.weixin"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <android.support.v4.view.ViewPager
  android:id="@+id/id_viewpager"
  android:layout_width="fill_parent"
  android:layout_height="0dp"
  android:layout_weight="1" >
 </android.support.v4.view.ViewPager>
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="60dp"
  android:background="@drawable/tabbg"
  android:orientation="horizontal" >
  <com.Sing.weixin.ChangeColorIconWithTextView
   android:id="@+id/id_indicator_one"
   android:layout_width="0dp"
   android:layout_height="fill_parent"
   android:layout_weight="1"
   android:padding="5dp"
   zhy:icon="@drawable/ic_menu_start_conversation"
   zhy:text="@string/tab_one"
   zhy:text_size="12sp" />
  <com.Sing.weixin.ChangeColorIconWithTextView
   android:id="@+id/id_indicator_two"
   android:layout_width="0dp"
   android:layout_height="fill_parent"
   android:layout_weight="1"
   android:padding="5dp"
   zhy:icon="@drawable/ic_menu_friendslist"
   zhy:text="@string/tab_two"
   zhy:text_size="12sp" />
 </LinearLayout>
</LinearLayout> 

MainActivity.java

package com.Sing.weixin;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
  OnPageChangeListener, OnClickListener {
 private ViewPager mViewPager;
 // 選項卡一Fragment
 private FrgReturnVisit returnVisit;
 // 選項卡一Fragment
 private FrgMicroVisit microVisit;
 private List<ChangeColorIconWithTextView> mTabIndicator = new ArrayList<ChangeColorIconWithTextView>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setOverflowShowingAlways();
  mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
  mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
  initTabIndicator();
  mViewPager.setOnPageChangeListener(this);
 }
 private void initTabIndicator() {
  ChangeColorIconWithTextView one = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_one);
  ChangeColorIconWithTextView two = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_two);
  mTabIndicator.add(one);
  mTabIndicator.add(two);
  one.setOnClickListener(this);
  two.setOnClickListener(this);
  one.setIconAlpha(1.0f);
 }
 @Override
 public void onPageSelected(int arg0) {
 }
 @Override
 public void onPageScrolled(int position, float positionOffset,
   int positionOffsetPixels) {
  if (positionOffset > 0) {
   ChangeColorIconWithTextView left = mTabIndicator.get(position);
   ChangeColorIconWithTextView right = mTabIndicator.get(position + 1);
   left.setIconAlpha(1 - positionOffset);
   right.setIconAlpha(positionOffset);
  }
 }
 @Override
 public void onPageScrollStateChanged(int state) {
 }
 @Override
 public void onClick(View v) {
  resetOtherTabs();
  switch (v.getId()) {
  case R.id.id_indicator_one:
   mTabIndicator.get(0).setIconAlpha(1.0f);
   mViewPager.setCurrentItem(0, false);
   break;
  case R.id.id_indicator_two:
   mTabIndicator.get(1).setIconAlpha(1.0f);
   mViewPager.setCurrentItem(1, false);
   break;
  }
 }
 /**
  * 重置其他的Tab
  */
 private void resetOtherTabs() {
  for (int i = 0; i < mTabIndicator.size(); i++) {
   mTabIndicator.get(i).setIconAlpha(0);
  }
 }
 private void setOverflowShowingAlways() {
  try {
   // true if a permanent menu key is present, false otherwise.
   ViewConfiguration config = ViewConfiguration.get(this);
   Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
   menuKeyField.setAccessible(true);
   menuKeyField.setBoolean(config, false);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public class MyPagerAdapter extends FragmentPagerAdapter {
  public MyPagerAdapter(FragmentManager fm) {
   super(fm);
  }
  private final String[] titles = { "選項一", "選項二"};
  @Override
  public CharSequence getPageTitle(int position) {
   return titles[position];
  }
  @Override
  public int getCount() {
   return titles.length;
  }
  @Override
  public Fragment getItem(int position) {
   switch (position) {
   case 0:
    if (returnVisit == null) {
     returnVisit = new FrgReturnVisit();
    }
    return returnVisit;
   case 1:
    if (microVisit == null) {
     microVisit = new FrgMicroVisit();
    }
    return microVisit;
   default:
    return null;
   }
  }
 }
} 

ChangeColorIconWithTextView.java

package com.Sing.weixin;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class ChangeColorIconWithTextView extends View {
 private Bitmap mBitmap;
 private Canvas mCanvas;
 private Paint mPaint;
 /**
  * 顏色
  */
 private int mColor = 0xFF45C01A;
 /**
  * 透明度 0.0-1.0
  */
 private float mAlpha = 0f;
 /**
  * 圖標
  */
 private Bitmap mIconBitmap;
 /**
  * 限制繪制icon的范圍
  */
 private Rect mIconRect;
 /**
  * icon底部文本
  */
 private String mText = "demo";
 private int mTextSize = (int) TypedValue.applyDimension(
   TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics());
 private Paint mTextPaint;
 private Rect mTextBound = new Rect();
 public ChangeColorIconWithTextView(Context context) {
  super(context);
 }
 /**
  * 初始化自定義屬性值
  * 
  * @param context
  * @param attrs
  */
 public ChangeColorIconWithTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // 獲取設置的圖標
  TypedArray a = context.obtainStyledAttributes(attrs,
    R.styleable.ChangeColorIconView);
  int n = a.getIndexCount();
  for (int i = 0; i < n; i++) {
   int attr = a.getIndex(i);
   switch (attr) {
   case R.styleable.ChangeColorIconView_icon:
    BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
    mIconBitmap = drawable.getBitmap();
    break;
   case R.styleable.ChangeColorIconView_color:
    mColor = a.getColor(attr, 0x45C01A);
    break;
   case R.styleable.ChangeColorIconView_text:
    mText = a.getString(attr);
    break;
   case R.styleable.ChangeColorIconView_text_size:
    mTextSize = (int) a.getDimension(attr, TypedValue
      .applyDimension(TypedValue.COMPLEX_UNIT_SP, 10,
        getResources().getDisplayMetrics()));
    break;
   }
  }
  a.recycle();
  mTextPaint = new Paint();
  mTextPaint.setTextSize(mTextSize);
  mTextPaint.setColor(0xff555555);
  // 得到text繪制范圍
  mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  // 得到繪制icon的寬
  int bitmapWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
    - getPaddingRight(), getMeasuredHeight() - getPaddingTop()
    - getPaddingBottom() - mTextBound.height());
  int left = getMeasuredWidth() / 2 - bitmapWidth / 2;
  int top = (getMeasuredHeight() - mTextBound.height()) / 2 - bitmapWidth
    / 2;
  // 設置icon的繪制范圍
  mIconRect = new Rect(left, top, left + bitmapWidth, top + bitmapWidth);
 }
 @Override
 protected void onDraw(Canvas canvas) {
  int alpha = (int) Math.ceil((255 * mAlpha));
  canvas.drawBitmap(mIconBitmap, null, mIconRect, null);
  setupTargetBitmap(alpha);
  drawSourceText(canvas, alpha);
  drawTargetText(canvas, alpha);
  canvas.drawBitmap(mBitmap, 0, 0, null);
 }
 private void setupTargetBitmap(int alpha) {
  mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
    Config.ARGB_8888);
  mCanvas = new Canvas(mBitmap);
  mPaint = new Paint();
  mPaint.setColor(mColor);
  mPaint.setAntiAlias(true);
  mPaint.setDither(true);
  mPaint.setAlpha(alpha);
  mCanvas.drawRect(mIconRect, mPaint);
  mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  mPaint.setAlpha(255);
  mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
 }
 private void drawSourceText(Canvas canvas, int alpha) {
  mTextPaint.setTextSize(mTextSize);
  mTextPaint.setColor(0xff333333);
  mTextPaint.setAlpha(255 - alpha);
  canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
    - mTextBound.width() / 2,
    mIconRect.bottom + mTextBound.height(), mTextPaint);
 }
 private void drawTargetText(Canvas canvas, int alpha) {
  mTextPaint.setColor(mColor);
  mTextPaint.setAlpha(alpha);
  canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
    - mTextBound.width() / 2,
    mIconRect.bottom + mTextBound.height(), mTextPaint);
 }
 public void setIconAlpha(float alpha) {
  this.mAlpha = alpha;
  invalidateView();
 }
 private void invalidateView() {
  if (Looper.getMainLooper() == Looper.myLooper()) {
   invalidate();
  } else {
   postInvalidate();
  }
 }
 public void setIconColor(int color) {
  mColor = color;
 }
 public void setIcon(int resId) {
  this.mIconBitmap = BitmapFactory.decodeResource(getResources(), resId);
  if (mIconRect != null)
   invalidateView();
 }
 public void setIcon(Bitmap iconBitmap) {
  this.mIconBitmap = iconBitmap;
  if (mIconRect != null)
   invalidateView();
 }
 private static final String INSTANCE_STATE = "instance_state";
 private static final String STATE_ALPHA = "state_alpha";
 @Override
 protected Parcelable onSaveInstanceState() {
  Bundle bundle = new Bundle();
  bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
  bundle.putFloat(STATE_ALPHA, mAlpha);
  return bundle;
 }
 @Override
 protected void onRestoreInstanceState(Parcelable state) {
  if (state instanceof Bundle) {
   Bundle bundle = (Bundle) state;
   mAlpha = bundle.getFloat(STATE_ALPHA);
   super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
  } else {
   super.onRestoreInstanceState(state);
  }
 }
} 

github:https://github.com/ganchuanpu/Demo_Sing_weixin

以上所述是小編給大家介紹的android 中viewpager+fragment仿微信底部TAG完美漸變效果,希望對大家有所幫助!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

鸡西市| 淳安县| 白山市| 吉隆县| 馆陶县| 泾川县| 武汉市| 青冈县| 巫山县| 孟州市| 高清| 拉孜县| 清流县| 思茅市| 庆元县| 威信县| 安达市| 吉首市| 宜君县| 宝山区| 明溪县| 上杭县| 潢川县| 宜阳县| 衡阳市| 溧阳市| 大田县| 佛教| 左权县| 视频| 娱乐| 曲靖市| 辉县市| 句容市| 军事| 望城县| 突泉县| 宁强县| 泉州市| 平山县| 呼玛县|