Android学习教程之高仿安卓微信6.0(2)

本文实例为大家分享了Android仿安卓微信6.0的具体代码,供大家参考,具体内容如下

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);
 }

 //初始化所有数据
 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;
 }

 //应用反射改变OverflowButton的图标
 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();
  }
 }

 //设置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);
 //内存准备Bitmap,setAlpha,纯色,xfermode,图标
 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);
 }

 //在内存中绘制可变色的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);
 //抖动处理
 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内容:

<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内容:

<resources>
 <string name="app_name">微信</string>
 <string name="action_search">查找</string>
 <string name="action_add">添加</string>
 <string name="menu_group_chat">发起群聊</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">发现</string>
 <string name="tab_me">我</string>

</resources>

main.xml内容 :

<?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内容:

<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资源:

运行结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现微信支付功能

    开发Android APP微信支付功能,需要完成三个步骤:第一步生成预支付订单.第二步生成微信支付参数.第三步调起微信APP支付.除了需要审核通过的APP应用外,还需要获得微信支付接口权限,然后获取对应的商户号.API密钥,这两者缺一不可,并且在APP微信支付中使用 获得商户号.API密钥 在微信开放平台中查看审核通过的APP应用,是否申请支付功能,若已申请,登录微信支付|商户平台:http://pay.weixin.qq.com 查看对应的商户号.API密钥 >申请微信支付接口 >登录商户平

  • Android高仿微信聊天界面代码分享

    微信聊天现在非常火,是因其界面漂亮吗,哈哈,也许吧.微信每条消息都带有一个气泡,非常迷人,看起来感觉实现起来非常难,其实并不难.下面小编给大家分享实现代码. 先给大家展示下实现效果图: OK,下面我们来看一下整个小项目的主体结构: 下面是Activity的代码: package com.way.demo; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import jav

  • Android仿QQ、微信聊天界面长按提示框效果

    先来看看效果图 如何使用 示例代码 PromptViewHelper pvHelper = new PromptViewHelper(mActivity); pvHelper.setPromptViewManager(new ChatPromptViewManager(mActivity)); pvHelper.addPrompt(holder.itemView.findViewById(R.id.textview_content)); 使用起来还是很简单的 首先new一个PromptViewH

  • android仿微信聊天界面 语音录制功能

    本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" andro

  • Android仿微信底部实现Tab选项卡切换效果

    在网上看了比较多的关于Tab的教程,发现都很杂乱.比较多的用法是用TitlePagerTabStrip和ViewPaper.不过TitlePagerTabStrip有个很大的缺陷,Tab里面的内容刚进去是没有的,要滑一次才能加载出来.而且滑动的时候,Tab里面的内容位置不是固定的,滑倒最后会出现一片空白,非常不美观.虽然有其他的补救方法,但是非常的麻烦,所以我就按照自己的方法实现了一个,功能不错而且非常简单. 直接点击或者是滑动界面,都可以转到相应的页面. 效果图: 原理是用了三个按钮和View

  • Android仿微信发表说说实现拍照、多图上传功能

    本文实例为大家分享了Android仿微信发表说说.心情功能,供大家参考,具体内容如下 既能实现拍照,选图库,多图案上传的案例,目前好多App都有类似微信朋友圈的功能,能过发表说说等附带图片上传.下面的就是实现该功能的过程:大家还没有看过Android Retrofit 2.0框架上传图片解决方案这篇文章,在看今天的就很容易,接在本项目中用到了一个library:photopicker,封装了图片的选择功能,是否选相机,还有选中图片后可以查看图片的功能.  一. 首先:将photopicker到工

  • Android仿微信主界面设计

    先来一张效果图 一.ActionBar的设计 首先是main.xml,先定义这些菜单,界面稍后在调整 <menu xmlns:android="http://schemas.android.com/apk/res/android" tools:context=".MainActivity"> <item android:id="@+id/action_search" android:actionViewClass="a

  • Android高仿微信5.2.1主界面及消息提醒

    好久没更新博客了,最近在做公司的项目,这也算是我接触的第一个正式项目.通过项目的检验,发现自己积累了一年的知识还是远远不够,想要提高,好的方法是 :项目+书+视频+博客.最重要一点:勤动手.最近发现了慕课网的视频,居然都是高清无码免费的!而且满满的干货!我用业余时间跟着视频中大神的讲解学习了不少知识,下面就将这些小demo与大家分享,当然,我做了一些优化,代码与视频中有些出入,但功能可以完全实现. 这是一个模仿5.2.1版本的显示界面,如下图所示: 功能及实现思路简介 主要功能很简单: 1.上面

  • Android App仿微信界面切换时Tab图标变色效果的制作方法

    概述 1.概述 学习Android少不了模仿各种app的界面,自从微信6.0问世以后,就觉得微信切换时那个变色的Tab图标屌屌的,今天我就带大家自定义控件,带你变色变得飞起~~ 好了,下面先看下效果图: 清晰度不太好,大家凑合看~~有木有觉得这个颜色弱爆了了的,,,下面我动动手指给你换个颜色: 有没有这个颜色比较妖一点~~~好了~下面开始介绍原理. 2.原理介绍 通过上面的效果图,大家可能也猜到了,我们的图标并非是两张图片,而是一张图,并且目标颜色是可定制的,谁让现在动不动就谈个性化呢. 那么我

  • Android仿微信图片点击全屏效果

    废话不多说,先看下效果: 先是微信的 再是模仿的 先说下实现原理,再一步步分析 这里总共有2个Activity一个就是主页,一个就是显示我们图片效果的页面,参数通过Intent传送,素材内容均来自网络,(感谢聪明的蘑菇) 图片都是Glide异步下的,下的,下的重要的事情说三次,然后就是用动画做放大操作然后显示出来了(并没有做下载原图的实现,反正也是一样 下载下来Set上去而且动画都不需要更简便). OK,我们来看分析下 obj,目录下分别创建了2个对象,一个用来使用来处理显示页面的图片尺寸信息以

随机推荐