这是一个新闻类的项目,由于工作原因只能是一点一点写,后续会补充完整,有疑惑可以私信我.
首先是首页面,欢迎页实现 旋转 缩放 渐变 的效果 ,三个引导页实现侧滑的同时底部的小点跟随侧滑移动.
GuideActivity.java
package com.example.lenovo.myx2_d1;
import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Created by LENOVO on 2018/08/01.
*/
public class GuideActivity extends Activity {
private LinearLayout dotLayout;
private ImageView redDot;
private ViewPager viewPager;
private int[] imgs = {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};
private int leftMargin =0 ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide_activity);
leftMargin = dip2px(this,10f);
initView();
initLoad();
initViewPager();
}
private List imgViews = new ArrayList<>();
private void initViewPager(){
// 把图片int集合遍历,同时创建多个ImageView,且设置图片,然后把imgView放入到imgViews集合里
for (int img :imgs){
ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(img);
imgViews.add(imgView);
}
MyPager myPager = new MyPager(imgViews);
viewPager.setAdapter(myPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int tempMargin = leftMargin;
int leftMar = (int) ((position*tempMargin)+(tempMargin*positionOffset));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) redDot.getLayoutParams();
params.leftMargin = leftMar*2;
redDot.setLayoutParams(params);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initView() {
dotLayout = findViewById(R.id.dot_layout);
redDot = findViewById(R.id.red_dot);
viewPager = findViewById(R.id.view_pager);
}
private void initLoad() {
// 添加三个点到Layout
for (int x = 0; x < imgs.length; x++) {
ImageView dot = new ImageView(this);
dot.setBackgroundResource(R.drawable.point_normal);
if (x != 0) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = leftMargin;
dot.setLayoutParams(params);
}
dotLayout.addView(dot);
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
MyPager.java
package com.example.lenovo.myx2_d1;
/**
* Created by LENOVO on 2018/08/01.
*/
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyPager extends PagerAdapter {
private List imgList;
public MyPager(List imgs){
if (imgList==null){
imgList = new ArrayList<>();
imgList.addAll(imgs);
}
}
@Override
public int getCount() {
return imgList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull // 添加子view==imageview,到container
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView img= imgList.get(position);
container.addView(img);
return img;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(imgList.get(position));
}
}
SplashActivity.java
package com.example.lenovo.myx2_d1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
/**
* Created by LENOVO on 2018/08/01.
*/
public class SplashActivity extends AppCompatActivity {
private RelativeLayout rootLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 实现 旋转 缩放 渐变 的效果
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setFillAfter(true);
ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setFillAfter(true);
RotateAnimation rotateAnimation=new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setFillAfter(true);
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.setDuration(2000);
rootLayout.startAnimation(animationSet);
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(SplashActivity.this,GuideActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void initView(){
rootLayout = findViewById(R.id.root_layout);
}
private void initLoad(){
}
}
以下是布局文件
activity_main.xml
content_main.xml
guide_activity.xml
values/style.xml
AndroidManifest.xml
链接:新闻项目