依赖
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.2'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.youth.banner:banner:1.1.5'
compile 'com.android.support:design:26.0.0-alpha1'
权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
动画的Activity 动画的时间过后跳转到下一页面
package com.example.administrator.gouwuche_20171219.view.activity;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.example.administrator.gouwuche_20171219.R;
import com.example.administrator.gouwuche_20171219.view.zidingyi_shitu.ProgressBarView;
public class SplashActivity extends AppCompatActivity {
private ProgressBarView pbv;
private int progress = 120;
private int time = 3;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//动画运行时间为3秒钟,动画结束后跳转到商品详情页面。
time--;
if (time == 0) {
startActivity(new Intent(SplashActivity.this, SecondActivity.class));
finish();
} else {
//设置动画播放进程
progress += 120;
pbv.setProgress(progress);
handler.sendEmptyMessageDelayed(0, 1000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.logo_img);
pbv = (ProgressBarView) findViewById(R.id.my_progess);
setAnimation(imageView);
handler.sendEmptyMessage(0);
pbv.setProgress(progress);
}
//执行动画的方法
private void setAnimation(ImageView imageView) {
//应用图标从屏幕最上方平移到屏幕中间
ObjectAnimator trans = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500f).setDuration(1000);
//缩放由2倍到1倍
ObjectAnimator scalX = ObjectAnimator.ofFloat(imageView, "scaleX", 2f, 1f).setDuration(1000);
ObjectAnimator scalY = ObjectAnimator.ofFloat(imageView, "scaleY", 2f, 1f).setDuration(1000);
//渐变从完全透明到完全不透明
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1f).setDuration(1000);
// 旋转为旋转一圈
ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(1000);
//动画组合开始执行
AnimatorSet setAnimatior = new AnimatorSet();
setAnimatior.play(trans).before(scalX).before(scalY).before(alpha).before(rotate);
setAnimatior.start();
}
}
SplashActivity
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.gouwuche_20171219.view.activity.SplashActivity">
<ImageView
android:layout_centerHorizontal="true"
android:id="@+id/logo_img"
android:src="@drawable/ic_launcher_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.administrator.gouwuche_20171219.view.zidingyi_shitu.ProgressBarView
android:visibility="gone"
android:id="@+id/my_progess"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
自定义类
package com.example.administrator.gouwuche_20171219.view.zidingyi_shitu;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2017/12/19.
*/
public class ProgressBarView extends View {
private Paint paint;
private int currentX = 100;
private int currentY = 100;
private int count;
private PointF pointF = new PointF(currentX,currentY);
private int mProgress;
public ProgressBarView(Context context) {
super(context);
initpaint(context);
}
private void initpaint(Context context) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
}
public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initpaint(context);
}
public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initpaint(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
canvas.drawCircle(pointF.x,pointF.y,20,paint);
canvas.drawCircle(pointF.x,pointF.y,30,paint);
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
RectF recyF = new RectF(75,75,125,125);
canvas.drawArc(recyF,-90,mProgress,false,paint);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawText(count+"",98,102,paint);
}
public void setProgress(int progress){
this.mProgress = progress;
if (mProgress == 120){
count = 2;
}
if (mProgress == 240){
count = 1;
}
if (mProgress == 360){
count = 0;
}
invalidate();
}
}
----以上是动画后跳转----
-----以下是详情页面----
详情页面类
package com.example.administrator.gouwuche_20171219.view.activity;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.gouwuche_20171219.R;
import com.example.administrator.gouwuche_20171219.model.bean.SecondBean;
import com.example.administrator.gouwuche_20171219.presenter.SecondPresenter;
import com.example.administrator.gouwuche_20171219.util.OkHttp3Util;
import com.example.administrator.gouwuche_20171219.view.interfac.SecondView;
import com.youth.banner.Banner;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class SecondActivity extends AppCompatActivity implements SecondView{
private Banner banner;
private SecondPresenter secondPresenter;
private TextView title;
private TextView yuanJia;
private TextView youHui;
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//图片
banner = findViewById(R.id.ProductImage);
//标题
title = findViewById(R.id.title1);
//原价
yuanJia = findViewById(R.id.yuanJia);
//优惠价
youHui = findViewById(R.id.youHui);
//实例化SecondPresenter 关联p层,获取数据
secondPresenter = new SecondPresenter(this);
secondPresenter.getData();
}
//购物车
public void goToCart(View view) {
//点击 购物车 按钮跳转至购物车列表页面
Intent intent1 = new Intent(SecondActivity.this, CartActivity.class);
startActivity(intent1);
}
//加入购物车
public void addCart(View view) {
//点击加入购物车的方法
Map<String, String> map=new HashMap<>();
map.put("uid",71+"");
map.put("pid",1+"");
OkHttp3Util.doPost("https://www.zhaoapi.cn/product/addCart", map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String body = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
//吐司加入购物车成功
Toast.makeText(SecondActivity.this, "购物车加入商品成功"+body, Toast.LENGTH_SHORT).show();
}
});
}
});
}
@Override
public void success(final SecondBean secondBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//设置图片 下标为0的图片数据
String images = secondBean.getData().getImages();
String[] split = images.split("\\|");
//轮播下面样式属性
banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);//设置圆形指示器与标题
banner.setIndicatorGravity(Banner.CENTER);//设置指示器位置
banner.setDelayTime(2000);//设置轮播时间
banner.isAutoPlay(false);
//设置图片集合
banner.setImages(split);//设置图片源
//设置商品信息显示
title.setText(secondBean.getData().getTitle());
yuanJia.setText("原价:¥" + secondBean.getData().getPrice());
//设置原价中间横线(删除线)
yuanJia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
youHui.setText("优惠价:" + secondBean.getData().getBargainPrice());
}
});
}
@Override
public void failed(Exception e) {
Toast.makeText(SecondActivity.this,"数据出错",Toast.LENGTH_SHORT).show();
}
}
----布局-----
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.gouwuche_20171219.view.activity.SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:id="@+id/relative01">
<ImageView
android:padding="5dp"
android:id="@+id/backImage"
android:layout_width="38dp"
android:layout_height="38dp"
android:src="@drawable/leftjiantou"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<TextView
android:padding="10dp"
android:text="商品详情"
android:textSize="26sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
<View
android:id="@+id/view"
android:background="#000"
android:layout_height="1dp"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_below="@+id/relative01"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="@+id/view"
android:layout_height="288dp"
android:id="@+id/relative">
<com.youth.banner.Banner
android:id="@+id/ProductImage"
android:layout_width="match_parent"
android:layout_height="match_parent" ></com.youth.banner.Banner>
</RelativeLayout>
<View
android:id="@+id/view01"
android:background="#000"
android:layout_height="1dp"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_below="@+id/relative"></View>
<LinearLayout
android:id="@+id/line1"
android:orientation="vertical"
android:layout_above="@+id/line2"
android:layout_width="match_parent"
android:layout_below="@+id/relative"
android:layout_height="wrap_content" >
<TextView
android:textSize="18sp"
android:id="@+id/title1"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_marginLeft="18dp"
android:layout_width="wrap_content" />
<TextView
android:textSize="18sp"
android:id="@+id/yuanJia"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_marginLeft="18dp"
android:layout_width="wrap_content" />
<TextView
android:textSize="18sp"
android:id="@+id/youHui"
android:layout_weight="1"
android:layout_height="0dp"
android:textColor="#f14d07"
android:layout_marginLeft="18dp"
android:layout_width="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_above="@+id/line2"
android:visibility="visible"
android:background="#000"
android:layout_height="1dp"></View>
<LinearLayout
android:id="@+id/line2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:onClick="goToCart"
android:text="购物车"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
></Button>
<Button
android:onClick="addCart"
android:gravity="center"
android:text="加入购物车"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</RelativeLayout>
---P层类----
package com.example.administrator.gouwuche_20171219.presenter;
import com.example.administrator.gouwuche_20171219.model.bean.SecondBean;
import com.example.administrator.gouwuche_20171219.model.SecondModel;
import com.example.administrator.gouwuche_20171219.presenter.interfac.SecondP;
import com.example.administrator.gouwuche_20171219.view.interfac.SecondView;
/**
* Created by Administrator on 2017/12/19.
*/
public class SecondPresenter implements SecondP{
SecondView secondView;
private SecondModel secondModel;
public SecondPresenter(SecondView secondView) {
this.secondView = secondView;
secondModel = new SecondModel(this);
}
public void getData() {
secondModel.getDataUrl();
}
@Override
public void success(SecondBean secondBean) {
secondView.success(secondBean);
}
@Override
public void failed(Exception e) {
secondView.failed(e);
}
}
-----P和View层接口----
public interface SecondP {
public void success(SecondBean secondBean);
public void failed(Exception e);
}
----Model类----
package com.example.administrator.gouwuche_20171219.model;
import com.example.administrator.gouwuche_20171219.model.bean.SecondBean;
import com.example.administrator.gouwuche_20171219.presenter.interfac.SecondP;
import com.example.administrator.gouwuche_20171219.util.OkHttp3Util;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by Administrator on 2017/12/19.
*/
public class SecondModel {
SecondP secondP;
public SecondModel(SecondP secondP) {
this.secondP = secondP;
}
public void getDataUrl() {
Map<String, String> map=new HashMap<>();
map.put("pid",1+"");
OkHttp3Util.doPost("https://www.zhaoapi.cn/product/getProductDetail", map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
secondP.failed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String string = response.body().string();
Gson gson = new Gson();
SecondBean secondBean = gson.fromJson(string, SecondBean.class);
secondP.success(secondBean);
}
}
});
}
}