Android仿微信界面
效果圖
原理介紹
1、先繪制一個顏色(例如:粉紅)
2、設置Mode=DST_IN
3、繪制我們這個可愛的小機器人
回答我,顯示什么,是不是顯示交集,交集是什么?交集是我們的小機器人的非透明區域,也就是那張臉,除了兩個眼;
好了,那怎么變色呢?
我繪制一個顏色的時候,難道不能設置alpha么
自定義圖標控件
自定義屬性
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="icon" format="reference" /> <attr name="color" format="color" /> <attr name="text" format="string" /> <attr name="text_size" format="dimension" /> <declare-styleable name="ChangeColorIconView"> <attr name="icon" /> <attr name="color" /> <attr name="text" /> <attr name="text_size" /> </declare-styleable> </resources>繪制圖標
繪制圖標有很多步驟呀,我來列一列
1、計算alpha(默認為0)
2、繪制原圖
3、在繪圖區域,繪制一個純色塊(設置了alpha),此步繪制在內存的bitmap上
4、設置mode,針對內存中的bitmap上的paint
5、繪制我們的圖標,此步繪制在內存的bitmap上
6、繪制原文本
7、繪制設置alpha和顏色后的文本
8、將內存中的bitmap繪制出來
根據上面的步驟,可以看出來,我們的圖標其實繪制了兩次,為什么要繪制原圖呢,因為我覺得比較好看。
3-5步驟,就是我們上面分析的原理
6-7步,是繪制文本,可以看到,我們的文本就是通過設置alpha實現的
MainActivity
package com.zhy.weixin6.ui;import java.lang.reflect.Field; import java.lang.reflect.Method; 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.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewConfiguration; import android.view.Window; import android.widget.SearchView; import android.widget.Toast;@SuppressLint("NewApi") public class MainActivity extends FragmentActivity implementsOnPageChangeListener, OnClickListener {private ViewPager mViewPager;private List<Fragment> mTabs = new ArrayList<Fragment>();private FragmentPagerAdapter mAdapter;private String[] mTitles = new String[] { "First Fragment!","Second Fragment!", "Third Fragment!", "Fourth Fragment!" };private List<ChangeColorIconWithTextView> mTabIndicator = new ArrayList<ChangeColorIconWithTextView>();@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setOverflowShowingAlways();getActionBar().setDisplayShowHomeEnabled(false);mViewPager = (ViewPager) findViewById(R.id.id_viewpager);initDatas();mViewPager.setAdapter(mAdapter);mViewPager.setOnPageChangeListener(this);}private void initDatas(){for (String title : mTitles){TabFragment tabFragment = new TabFragment();Bundle args = new Bundle();args.putString("title", title);tabFragment.setArguments(args);mTabs.add(tabFragment);}mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){@Overridepublic int getCount(){return mTabs.size();}@Overridepublic Fragment getItem(int arg0){return mTabs.get(arg0);}};initTabIndicator();}SearchView searchView;@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.main, menu);MenuItem searchItem = menu.findItem(R.id.action_search); searchView = (SearchView) searchItem.getActionView(); return true;}@Overridepublic boolean onSearchRequested() {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), searchView.getQuery(), 0).show();return super.onSearchRequested();}private void initTabIndicator(){ChangeColorIconWithTextView one = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_one);ChangeColorIconWithTextView two = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_two);ChangeColorIconWithTextView three = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_three);ChangeColorIconWithTextView four = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_four);mTabIndicator.add(one);mTabIndicator.add(two);mTabIndicator.add(three);mTabIndicator.add(four);one.setOnClickListener(this);two.setOnClickListener(this);three.setOnClickListener(this);four.setOnClickListener(this);one.setIconAlpha(1.0f);}@Overridepublic void onPageSelected(int arg0){}@Overridepublic void onPageScrolled(int position, float positionOffset,int positionOffsetPixels){// Log.e("TAG", "position = " + position + " , positionOffset = "// + positionOffset);if (positionOffset > 0){ChangeColorIconWithTextView left = mTabIndicator.get(position);ChangeColorIconWithTextView right = mTabIndicator.get(position + 1);left.setIconAlpha(1 - positionOffset);right.setIconAlpha(positionOffset);}}@Overridepublic void onPageScrollStateChanged(int state){}@Overridepublic 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;case R.id.id_indicator_three:mTabIndicator.get(2).setIconAlpha(1.0f);mViewPager.setCurrentItem(2, false);break;case R.id.id_indicator_four:mTabIndicator.get(3).setIconAlpha(1.0f);mViewPager.setCurrentItem(3, false);break;}}/*** 重置其他的Tab*/private void resetOtherTabs(){for (int i = 0; i < mTabIndicator.size(); i++){mTabIndicator.get(i).setIconAlpha(0);}}@Overridepublic boolean onMenuOpened(int featureId, Menu menu){if (featureId == Window.FEATURE_ACTION_BAR && menu != null){if (menu.getClass().getSimpleName().equals("MenuBuilder")){try{Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);m.setAccessible(true);m.invoke(menu, true);} catch (Exception e){}}}return super.onMenuOpened(featureId, menu);}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();}}}Activity里面代碼雖然沒什么注釋,但是很簡單哈,就是初始化Fragment,得到我們的適配器,然后設置給ViewPager;
initTabIndicator我們初始化我們的自定義控件,以及加上了點擊事件;
唯一一個需要指出的就是:
我們在onPageScrolled中,動態的獲取position以及positionOffset,然后拿到左右兩個View,設置positionOffset ;
兩個反射的方法,是控制Actionbar的圖標的,和點擊menu按鍵顯示的
TabFragment
package com.zhy.weixin6.ui; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class TabFragment extends Fragment { private String mTitle = "Default"; public TabFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (getArguments() != null) { mTitle = getArguments().getString("title"); } TextView textView = new TextView(getActivity()); textView.setTextSize(20); textView.setBackgroundColor(Color.parseColor("#ffffffff")); textView.setGravity(Gravity.CENTER); textView.setText(mTitle); return textView; } }menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_search"android:actionViewClass="android.widget.SearchView"android:icon="@drawable/actionbar_search_icon"android:showAsAction="ifRoom|collapseActionView"android:title="@string/action_search"/><itemandroid:id="@+id/action_group_chat"android:icon="@drawable/ofm_group_chat_icon"android:title="@string/action_group_chat"/><itemandroid:id="@+id/action_add_friend"android:icon="@drawable/ofm_add_icon"android:title="@string/action_add_friend"/><itemandroid:id="@+id/action_scan"android:icon="@drawable/ofm_qrcode_icon"android:title="@string/action_scan"/><itemandroid:id="@+id/action_feed"android:icon="@drawable/ofm_feedback_icon"android:title="@string/action_feed"/></menu>源碼下載
源碼下載
參考鏈接
Android 高仿微信6.0主界面 帶你玩轉切換圖標變色 - Hongyang - 博客頻道 - CSDN.NET
http://blog.csdn.net/lmj623565791/article/details/41087219
總結
以上是生活随笔為你收集整理的Android仿微信界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ShareSDK集成微信、QQ、微博分享
- 下一篇: MySQL流浪记(一)—— 初步认识数据