写这个不是因为这个很难,而是发现特么每次新写一个项目都要写这些东西,不如就把他们拿出来,以后可以省下不少时间。
1:启动页面 一个动画 然后进入app
2:第一次打开的引导页面
3:登陆界面(当然这块ui会有不同需求,但是改吧改吧就行了)
4:注册界面(同登陆界面)
5:主界面
6:一些通用控件(这里就放了一个dialog,下拉刷新上拉加载控件)
基本就这些内容,每次都写或者拷贝一次,也蛮浪费时间的,下面就一步一步放上来,也希望对有些刚工作同志一些帮助
这里就简单放了一个 颜色渐变的动画,置于有啥特殊需求自己改吧,涉及知识点也就是一个渐变动画了
public class SplashActivity extends BaseActivity {
private Animation myAnimation_Alpha;
private RelativeLayout rl_homepage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
rl_homepage = (RelativeLayout) findViewById(R.id.rl_homepage);
//根据各自的构造方法来初始化一个实例对象
myAnimation_Alpha=new AlphaAnimation(0.3f, 1.0f);
rl_homepage.setAnimation(myAnimation_Alpha);
myAnimation_Alpha.setDuration(3000);
myAnimation_Alpha.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(getApplication(),GuideActivity.class);
startActivityWithAnim(intent);
finish();
}
});
myAnimation_Alpha.startNow();
}
}
这块内容很简单,就这些,注意里面用了一个方法
startActivityWithAnim
这个是放在我的baseactivity里面的,我们通常在里面封装一些常用的方法,例如切换页面动画等等,我就在里面放了一个左边退出右边进入的动画,当然在使用baseactivity的activity里面,需要页面有动画,要使用这两个有动画效果的方法
public class BaseActivity extends FragmentActivity {
TextView tv_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv_title = (TextView) findViewById(R.id.tv_title);
}
//如果有header 直接绑定返回按钮点击事件
public void backpressed(View view){
finishWithAnim();
}
protected void setTitle(String title){
if(tv_title!=null){
tv_title.setText(title);
}
}
/***
* 带动画启动 activity
* @param intent
*/
protected void startActivityWithAnim(Intent intent){
startActivity(intent);
overridePendingTransition(R.anim.default_fromright_in,R.anim.default_toleft_out);
}
/***
* 带动画退出 activity
*/
protected void finishWithAnim(){
finish();
overridePendingTransition(R.anim.default_fromright_in,R.anim.default_toleft_out);
}
}
里面有4个 anim文件,下面给出来
default_fromleft_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="-100%p"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0" />
</set>
default_fromright_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="100%p"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0" />
</set>
default_toleft_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-100%p"
android:toYDelta="0" />
</set>
default_toright_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="0" />
</set>
第一个启动界面完事儿了,然后进入引导页面
引导页主要就是ViewPager切换页面,到最后一页的时候给一个按钮,进入应用,有些需要有下面四个小圆点要求样子可能不一样,就自己改吧改吧了
public class GuideActivity extends BaseActivity implements OnPageChangeListener {
/**
* ViewPager
*/
private ViewPager viewPager;
private ImageView[] tips;
private ImageView[] mImageViews;
private int[] imgIdArray;
private Button bt_enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
bt_enter = (Button) findViewById(R.id.bt_enter);
viewPager = (ViewPager) findViewById(R.id.viewPager);
bt_enter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
startActivityWithAnim(intent);
finish();
}
});
imgIdArray = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4 };
tips = new ImageView[imgIdArray.length];
for (int i = 0; i < tips.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(10, 10));
tips[i] = imageView;
if (i == 0) {
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layoutParams.leftMargin = 15;
layoutParams.rightMargin = 15;
layoutParams.bottomMargin = 20;
group.addView(imageView, layoutParams);
}
mImageViews = new ImageView[imgIdArray.length];
for (int i = 0; i < mImageViews.length; i++) {
ImageView imageView = new ImageView(this);
mImageViews[i] = imageView;
imageView.setBackgroundResource(imgIdArray[i]);
}
viewPager.setAdapter(new MyAdapter());
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(0);
}
/**
*
* @author xiaanming
*
*/
public class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return imgIdArray.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(mImageViews[position
% mImageViews.length]);
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(mImageViews[position
% mImageViews.length], 0);
return mImageViews[position % mImageViews.length];
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
setImageBackground(arg0 % mImageViews.length);
if(arg0 == (mImageViews.length-1)){
bt_enter.setVisibility(View.VISIBLE);
}else{
bt_enter.setVisibility(View.GONE);
}
}
/**
*
* @param selectItems
*/
private void setImageBackground(int selectItems) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItems) {
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
}
}
@Override
public void onBackPressed() {
finishWithAnim();
super.onBackPressed();
}
}
布局文件也很简单
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
<Button
android:id="@+id/bt_enter"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:visibility="gone"
android:text="进入应用 "/>
</RelativeLayout>
然后就是登陆注册界面
登陆界面注册界面这两个变数会比较大,这边就大概放的有通用的会着重讲一下
注册的时候有个同意条款,这个还是比较通用的
这块详细代码就不贴了,意义也不大,后面会把项目发上来,这里单独把那个同意协议的 代码贴一下
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:button="@drawable/checkbox_selector"
android:text=" 我已阅读并同意" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用条款和隐私政策"
android:textColor="@color/themecolor" />
</LinearLayout>
checkbox_selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/ic_item_unselect" />
<item android:state_selected="true" android:drawable="@drawable/ic_item_unselect"></item>
<item android:state_pressed="true" android:drawable="@drawable/ic_item_unselect"></item>
<item android:state_checked="false" android:drawable="@drawable/ic_item_select" />
</selector>
行了这块完事儿了
然后就是主界面
主界面目前就是放了个 tabhost 和fragment 的底部导航栏,当然你们要的什么效果可以自己去写,这里不可能所有需求都放里面,这显然是不可能的
上拉下拉 listview 也就不写了
然后 一个稍微好看一些的dialog是必不可少的
代码就不一一贴了,特么太麻烦 ,项目自己下载哦