Android學(xué)習(xí)教程之高仿安卓微信6.0(2)
本文實(shí)例為大家分享了Android仿安卓微信6.0的具體代碼,供大家參考,具體內(nèi)容如下
wechat6Activity.java的代碼:
package siso.geekworld;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import viewhelper.ChangeColorIconWithText;
import viewhelper.TabFragment;
public class wechat6Activity extends FragmentActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
private ViewPager viewPager;
private List<Fragment> mTabs = new ArrayList<>();
private String[] mTitles = new String[]{"First Fragment","Second Fragment","Third Fragment","Fourth Fragment"};
private FragmentPagerAdapter adapter;
private List<ChangeColorIconWithText> mTabIndicators = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wechat6);
setOverflowButtonAlways();
getActionBar().setDisplayShowHomeEnabled(false);
initView();
initDatas();
initEvents();
viewPager.setAdapter(adapter);
}
//初始化所有事件
private void initEvents() {
viewPager.addOnPageChangeListener(this);
}
//初始化所有數(shù)據(jù)
private void initDatas() {
for(String mtitle:mTitles){
TabFragment tabFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString(TabFragment.TITLE,mtitle);
tabFragment.setArguments(bundle);
mTabs.add(tabFragment);
}
adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mTabs.get(position);
}
@Override
public int getCount() {
return mTabs.size();
}
};
}
//初始化所有view
private void initView() {
viewPager = (ViewPager)findViewById(R.id.id_viewpager);
ChangeColorIconWithText one = (ChangeColorIconWithText)findViewById(R.id.id_indicator_one);
ChangeColorIconWithText two = (ChangeColorIconWithText)findViewById(R.id.id_indicator_two);
ChangeColorIconWithText three = (ChangeColorIconWithText)findViewById(R.id.id_indicator_three);
ChangeColorIconWithText four = (ChangeColorIconWithText)findViewById(R.id.id_indicator_four);
mTabIndicators.add(one);
mTabIndicators.add(two);
mTabIndicators.add(three);
mTabIndicators.add(four);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
one.setIconAlpha(1.0f);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//應(yīng)用反射改變OverflowButton的圖標(biāo)
private void setOverflowButtonAlways(){
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKey = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
menuKey.setAccessible(true);
menuKey.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
//設(shè)置menu顯示icon
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId== Window.FEATURE_ACTION_BAR&&menu!=null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu,true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
@Override
public void onClick(View v) {
resetOtherTabs();
switch (v.getId()){
case R.id.id_indicator_one:
mTabIndicators.get(0).setIconAlpha(1.0f);
viewPager.setCurrentItem(0,false);
break;
case R.id.id_indicator_two:
mTabIndicators.get(1).setIconAlpha(1.0f);
viewPager.setCurrentItem(1, false);
break;
case R .id.id_indicator_three:
mTabIndicators.get(2).setIconAlpha(1.0f);
viewPager.setCurrentItem(2, false);
break;
case R.id.id_indicator_four:
mTabIndicators.get(3).setIconAlpha(1.0f);
viewPager.setCurrentItem(3, false);
break;
}
}
private void resetOtherTabs() {
for(int i=0;i<mTabIndicators.size();i++){
mTabIndicators.get(i).setIconAlpha(0);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(positionOffset>0){
ChangeColorIconWithText left = mTabIndicators.get(position);
ChangeColorIconWithText right = mTabIndicators.get(position+1);
left.setIconAlpha(1-positionOffset);
right.setIconAlpha(positionOffset);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
ChangeColorIconWithText.java代碼:
package viewhelper;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
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;
import siso.geekworld.R;
public class ChangeColorIconWithText extends View {
private int mColor = 0xFF45C01A;
private String mText = "微信";
private Bitmap mIconBitmap;
private int mTextSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,12,
getResources().getDisplayMetrics());
private Canvas mCanvas;
private Bitmap mBitmap;
private Paint mPaint;
//透明度0.0~1.0
private float mAlpha ;
private Rect mIconRect;
private Rect mTextBound;
private Paint mTextPaint;
private static final String INSTANCE_STATUS = "instance_status";
private static final String INSTANCE_ALPHA = "instance_alpha";
public ChangeColorIconWithText(Context context) {
this(context, null);
}
public ChangeColorIconWithText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 獲取自定義屬性的值
* @param context
* @param attrs
* @param defStyleAttr
*/
public ChangeColorIconWithText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorIconWithText);
int n = a.getIndexCount();
for(int i=0;i<n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.ChangeColorIconWithText_micon:
BitmapDrawable drawable =(BitmapDrawable)a.getDrawable(attr);
mIconBitmap = drawable.getBitmap();
break;
case R.styleable.ChangeColorIconWithText_mcolor:
mColor = a.getColor(attr,0xFF45C01A);
break;
case R.styleable.ChangeColorIconWithText_text:
mText = a.getString(attr);
break;
case R.styleable.ChangeColorIconWithText_text_size:
mTextSize = (int)a.getDimension(attr,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,12,
getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
mTextBound = new Rect();
mTextPaint = new Paint();
mTextPaint.setTextSize(mTextSize);
mTextPaint.setColor(0Xff555555);
mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int iconWidth = Math.min(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - mTextBound.height());
int left = getMeasuredWidth()/2 - iconWidth/2;
int top = getMeasuredHeight()/2-(mTextBound.height()+iconWidth)/2;
mIconRect = new Rect(left,top,left+iconWidth,top+iconWidth);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mIconBitmap, null, mIconRect, null);
int alpha = (int)Math.ceil(255*mAlpha);
//內(nèi)存準(zhǔn)備Bitmap,setAlpha,純色,xfermode,圖標(biāo)
setUpTargetBitmap(alpha);
//1.繪制原文本 2.繪制變色的文本
drawSourceText(canvas,alpha);
drawTargetText(canvas,alpha);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
//繪制變色的文本
private void drawTargetText(Canvas canvas, int alpha) {
mTextPaint.setColor(mColor);
mTextPaint.setAlpha(alpha);
int x = mIconRect.left+mIconRect.width()/2-mTextBound.width()/2;
int y = mIconRect.bottom+mTextBound.height();
canvas.drawText(mText, x, y, mTextPaint);
}
//繪制原文本
private void drawSourceText(Canvas canvas, int alpha) {
mTextPaint.setColor(0xff333333);
mTextPaint.setAlpha(255-alpha);
int x = mIconRect.left+mIconRect.width()/2-mTextBound.width()/2;
int y = mIconRect.bottom+mTextBound.height();
canvas.drawText(mText, x, y, mTextPaint);
}
//在內(nèi)存中繪制可變色的Icon
private void setUpTargetBitmap(int alpha) {
mBitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPaint = new Paint();
mPaint.setColor(mColor);
//抗鋸齒
mPaint.setAntiAlias(true);
//抖動(dòng)處理
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);
}
public void setIconAlpha(float alpha){
this.mAlpha = alpha;
//重繪
invalidateView();
}
private void invalidateView(){
if(Looper.getMainLooper()==Looper.myLooper()){
//是UI線程
invalidate();
}else{
postInvalidate();
}
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(INSTANCE_STATUS,super.onSaveInstanceState());
bundle.putFloat(INSTANCE_ALPHA,mAlpha);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(state instanceof Bundle){
Bundle bundle = (Bundle)state;
mAlpha = bundle.getFloat(INSTANCE_ALPHA);
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));
}else{
super.onRestoreInstanceState(state);
}
}
}
TabFragment.java代碼:
package viewhelper;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TabFragment extends android.support.v4.app.Fragment {
private String mTitle = "DEFAULT";
public static final String TITLE = "title";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(getArguments()!=null){
mTitle = getArguments().getString(TITLE);
}
TextView tv = new TextView(getActivity());
tv.setText(mTitle);
tv.setTextSize(20);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.parseColor("#ffffffff"));
return tv;
}
}
activity_wechat6.xml內(nèi)容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hymen="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/id_viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </android.support.v4.view.ViewPager> <LinearLayout android:orientation="horizontal" android:background="@drawable/tab_bg" android:layout_width="match_parent" android:layout_height="60dp"> <viewhelper.ChangeColorIconWithText android:id="@+id/id_indicator_one" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:padding="5dp" hymen:micon="@drawable/ic_menu_start_conversation" hymen:mcolor="#ff45c01a" hymen:text_size="12sp" hymen:text="@string/app_name" /> <viewhelper.ChangeColorIconWithText android:id="@+id/id_indicator_two" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:padding="5dp" hymen:micon="@drawable/ic_menu_friendslist" hymen:mcolor="#ff45c01a" hymen:text_size="12sp" hymen:text="@string/tab_contact" /> <viewhelper.ChangeColorIconWithText android:id="@+id/id_indicator_three" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:padding="5dp" hymen:mcolor="#ff45c01a" hymen:micon="@drawable/ic_menu_emoticons" hymen:text="@string/tab_found" hymen:text_size="12sp" /> <viewhelper.ChangeColorIconWithText android:id="@+id/id_indicator_four" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:padding="5dp" hymen:mcolor="#ff45c01a" hymen:micon="@drawable/ic_menu_allfriends" hymen:text="@string/tab_me" hymen:text_size="12sp" /> </LinearLayout> </LinearLayout>
strings.xml內(nèi)容:
<resources> <string name="app_name">微信</string> <string name="action_search">查找</string> <string name="action_add">添加</string> <string name="menu_group_chat">發(fā)起群聊</string> <string name="menu_feedback">意見反饋</string> <string name="menu_addfriend">添加朋友</string> <string name="menu_scan">掃一掃</string> <string name="tab_contact">通訊錄</string> <string name="tab_found">發(fā)現(xiàn)</string> <string name="tab_me">我</string> </resources>
main.xml內(nèi)容 :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:icon="@drawable/actionbar_search_icon" android:actionViewClass="android.widget.SearchView" android:showAsAction="ifRoom|collapseActionView" android:title="@string/action_search"/> <item android:id="@+id/action_group_chat" android:icon="@drawable/menu_group_chat_icon" android:title="@string/menu_group_chat"/> <item android:id="@+id/action_add_friend" android:icon="@drawable/menu_add_icon" android:title="@string/menu_addfriend"/> <item android:id="@+id/action_scan" android:icon="@drawable/men_scan_icon" android:title="@string/menu_scan"/> <item android:id="@+id/action_feedback" android:icon="@drawable/menu_feedback_icon" android:title="@string/menu_feedback"/> </menu>
styles.xml內(nèi)容:
<resources> <!-- Base application theme. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:actionOverflowButtonStyle">@style/WeiXinOverflowButtonStyle</item> </style> <style name="WeiXinOverflowButtonStyle"> <item name="android:src">@drawable/actionbar_add_icon</item> </style> <!-- Application theme. --> <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- <style name="AppTheme" parent="AppBaseTheme">--> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>
drawable資源:

運(yùn)行結(jié)果如圖:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
清楚詳解Android?進(jìn)程間圖傳遞圖形buffer原理
這篇文章主要為大家清楚的詳解了Android?進(jìn)程間圖傳遞圖形buffer原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android Metro菜單實(shí)現(xiàn)思路及代碼
在安卓平臺(tái)上實(shí)現(xiàn)一下Metro菜單效果,之前有介紹過了,相信大家對此不會(huì)陌生了吧,感興趣的朋友可以了解下哈2013-06-06
Flutter實(shí)現(xiàn)底部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部導(dǎo)航欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
音量控制鍵控制的音頻流(setVolumeControlStream)描述
當(dāng)開發(fā)多媒體應(yīng)用或者游戲應(yīng)用的時(shí)候,需要使用音量控制鍵來設(shè)置程序的音量大小,在Android系統(tǒng)中有多種音頻流,感興趣的朋友可以了解下2013-01-01
Android實(shí)現(xiàn)簡單的撥號(hào)器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單的撥號(hào)器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼
這篇文章主要介紹了Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android Studio 利用Splash制作APP啟動(dòng)界面的方法
這篇文章主要介紹了Android Studio 利用Splash制作APP啟動(dòng)界面,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android中post請求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例
下面小編就為大家分享一篇Android中post請求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

