您的位置:首页 > 移动开发 > 微信开发

Android 仿微信的应用进入界面

2014-11-05 15:05 211 查看

Android 仿微信的应用进入界面

不会制作动画,简易给出效果吧







OK 大致效果如图

给出xml文件
<?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mainRLayout" android:background="#000000">
<ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/show" />
- <com.yyy.weixin.MyScrollLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="visible">
- <RelativeLayout android:background="@drawable/w01" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="89dp" android:text="微信,不只是个聊天工具" android:textColor="@color/TextColor" android:textSize="18sp" />
</RelativeLayout>
- <RelativeLayout android:background="@drawable/w02" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="96dp" android:text="第一次,你可以使用透明背景的动画表情,来表达你此刻的心情" android:gravity="center_horizontal" android:textColor="@color/TextColor" android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout android:background="@drawable/w03" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:background="@drawable/w04" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:background="@drawable/w05" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:background="@drawable/w06" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:background="@drawable/w07" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:background="@drawable/w08" android:layout_width="fill_parent" android:layout_height="fill_parent" />
- <RelativeLayout android:background="@drawable/w01" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/startBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="98dp" android:text="开始我的微信生活" android:textSize="22sp" android:textColor="@color/TextColor" android:background="@drawable/button_bg" android:layout_gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="15dp" android:paddingBottom="15dp" />
</RelativeLayout>
</com.yyy.weixin.MyScrollLayout>
- <LinearLayout android:orientation="horizontal" android:id="@+id/llayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="24.0dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:visibility="visible">
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
<ImageView android:clickable="true" android:padding="5.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/page_indicator_bg" />
</LinearLayout>
- <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:id="@+id/animLayout">
- <LinearLayout android:id="@+id/leftLayout" android:layout_width="wrap_content" android:layout_height="fill_parent">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/whatsnew_left" />
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/whatsnew_left_m" android:layout_marginLeft="-3dp" />
</LinearLayout>
- <LinearLayout android:id="@+id/rightLayout" android:layout_width="wrap_content" android:layout_height="fill_parent">
<ImageView android:layout_height="fill_parent" android:layout_width="wrap_content" android:src="@drawable/whatsnew_right_m" />
<ImageButton android:layout_height="fill_parent" android:layout_width="wrap_content" android:src="@drawable/whatsnew_right" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
在这里,我们需要重点关注啊
<com.yyy.weixin.MyScrollLayout>
<LinearLayout
android:id="@+id/llayout">
他们构成了整个界面的核心
<com.yyy.weixin.MyScrollLayout>构成了左右滑动切换界面

android:id="@+id/llayout" 构成了9个相互装换的小节点

import android.R.bool;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.Scroller;

public class MyScrollLayout extends ViewGroup {

private static final String TAG = "ScrollLayout";
private VelocityTracker mVelocityTracker; // 用于判断甩动手势
private static final int SNAP_VELOCITY = 600;   //X轴速度基值,大于该值时进行切换
private Scroller mScroller; // 滑动控制器	private int mCurScreen;   //当前页面为第几屏
private int mDefaultScreen = 0;
private float mLastMotionX;

private OnViewChangeListener mOnViewChangeListener;

public MyScrollLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}

public MyScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}

private void init(Context context) {
mCurScreen = mDefaultScreen;
mScroller = new Scroller(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub

Log.e("a", "a");
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();
Log.e("count", childCount+"");
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

Log.e("b", "b");
final int width = MeasureSpec.getSize(widthMeasureSpec);

final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}

scrollTo(mCurScreen * width, 0);

}

public void snapToDestination() {
final int screenWidth = getWidth();

final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
snapToScreen(destScreen);
}

//使屏幕移动到第whichScreen+1屏
public void snapToScreen(int whichScreen) {

if (getScrollX() != (whichScreen * getWidth())) {
final int delta = whichScreen * getWidth() - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
mCurScreen = whichScreen;
invalidate();

if (mOnViewChangeListener != null) {
mOnViewChangeListener.OnViewChange(mCurScreen);
}
}
}

@Override
public void computeScroll() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();

switch (action) {
case MotionEvent.ACTION_DOWN:

Log.i("", "onTouchEvent  ACTION_DOWN");
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
}
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
break;

case MotionEvent.ACTION_MOVE:

int deltaX = (int) (mLastMotionX - x);
if (IsCanMove(deltaX)) {
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
}
mLastMotionX = x;
//正向或者负向移动,屏幕跟随手指移动

scrollBy(deltaX, 0);
}
break;

case MotionEvent.ACTION_UP:

int velocityX = 0;
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
//得到X轴方向手指移动速度
velocityX = (int) mVelocityTracker.getXVelocity();
}
//velocityX为正值说明手指向右滑动,为负值说明手指向左滑动
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
Log.e(TAG, "snap left");
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
Log.e(TAG, "snap right");
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}

if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}

break;
}
return true;
}

private boolean IsCanMove(int deltaX) {
//deltaX<0说明手指向右划
if (getScrollX() <= 0 && deltaX < 0) {
return false;
}
//deltaX>0说明手指向左划
if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) {
return false;
}
return true;
}

public void SetOnViewChangeListener(OnViewChangeListener listener) {
mOnViewChangeListener = listener;
}

}


public interface OnViewChangeListener {
public void OnViewChange(int view);
}


?
package com.yyy.weixin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class TestWeiXinOpen extends Activity implements OnViewChangeListener{

private MyScrollLayout mScrollLayout;
private ImageView[] imgs;
private int count;
private int currentItem;
private Button startBtn;
private RelativeLayout mainRLayout;
private LinearLayout pointLLayout;
private LinearLayout leftLayout;
private LinearLayout rightLayout;
private LinearLayout animLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}

private void initView() {
mScrollLayout  = (MyScrollLayout) findViewById(R.id.ScrollLayout);
pointLLayout = (LinearLayout) findViewById(R.id.llayout);
mainRLayout = (RelativeLayout) findViewById(R.id.mainRLayout);
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(onClick);
animLayout = (LinearLayout) findViewById(R.id.animLayout);
leftLayout  = (LinearLayout) findViewById(R.id.leftLayout);
rightLayout  = (LinearLayout) findViewById(R.id.rightLayout);
count = mScrollLayout.getChildCount();
imgs = new ImageView[count];
for(int i = 0; i< count;i++) {
imgs[i] = (ImageView) pointLLayout.getChildAt(i);
imgs[i].setEnabled(true);
imgs[i].setTag(i);
}
currentItem = 0;
imgs[currentItem].setEnabled(false);
mScrollLayout.SetOnViewChangeListener(this);
}

private View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startBtn:
mScrollLayout.setVisibility(View.GONE);
pointLLayout.setVisibility(View.GONE);
animLayout.setVisibility(View.VISIBLE);
mainRLayout.setBackgroundResource(R.drawable.whatsnew_bg);
Animation leftOutAnimation = AnimationUtils.loadAnimation(TestWeiXinOpen.this, R.anim.translate_left);
Animation rightOutAnimation = AnimationUtils.loadAnimation(TestWeiXinOpen.this, R.anim.translate_right);
leftLayout.startAnimation(leftOutAnimation);
rightLayout.startAnimation(rightOutAnimation);
leftOutAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mainRLayout.setBackgroundColor(R.color.bgColor);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
leftLayout.setVisibility(View.GONE);
rightLayout.setVisibility(View.GONE);
}
});
break;
}
}
};

@Override
public void OnViewChange(int position) {
setcurrentPoint(position);
}

private void setcurrentPoint(int position) {
if(position < 0 || position > count -1 || currentItem == position) {
return;
}
imgs[currentItem].setEnabled(true);
imgs[position].setEnabled(false);
currentItem = position;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐