需要用到的依赖
useLibrary 'org.apache.http.legacy'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.google.guava:guava:16.0.1'
implementation 'com.squareup.picasso:picasso:2.3.2'
//mainactivity
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {
private ImageView imt;
private RotateAnimation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullScreen();
setContentView(R.layout.activity_main);
//获取资源ID
imt = findViewById(R.id.im_t);
rotateAnim();
}
public void rotateAnim(){//动画的旋转
animation = new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setFillAfter(true);// 设置保持动画最后的状态
animation.setDuration(2000);// 设置动画时间
animation.setInterpolator(new AccelerateInterpolator());// 设置插入器
animation.setAnimationListener(this);
imt.startAnimation(animation);//启动动画旋转
}
/**
* 设置全屏
*/
private void setFullScreen() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
@Override
public void onAnimationStart(Animation animation) {
}
/*
动画结束调用
*/
@Override
public void onAnimationEnd(Animation animation) {
toMain();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
/**
* 跳转到首页
*/
private void toMain() {
startActivity(new Intent(this, TwoActivity.class));
finish();
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
//Twoactivity 轮播图+GridView展示
public class TwoActivity extends AppCompatActivity {
private static final int CODE_BANNER = 1;
private static final long DELAY = 2000;
private static final String TAG = MainActivity.class.getSimpleName();
private Toolbar toolbar;
private ViewPager viewPager;
private LinearLayout llPoints;
private GridView gridView;
private List<Bean.ResultBean.PicsBean> pics;
private List<Bean.ResultBean.ProductBean> product;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
setFullScreen();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toolbar = findViewById(R.id.tool);
toolbar.setTitle("");
viewPager = findViewById(R.id.vp);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
switch (state){
case ViewPager.SCROLL_STATE_IDLE:
startBanner ();
break;
case ViewPager.SCROLL_STATE_SETTLING:
break;
case ViewPager.SCROLL_STATE_DRAGGING:
handler.removeCallbacksAndMessages (null);
break;
}
}
});
//
llPoints = findViewById (R.id.lin_Po);
gridView = findViewById(R.id.g);
//监听gridView
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(TwoActivity.this,adapter.getItem(position).getName(),Toast.LENGTH_SHORT).show();
}
});
getDataFromServer ();
}
private void startBanner() {
handler.sendEmptyMessageDelayed(CODE_BANNER,DELAY);
}
private void getDataFromServer() {
new HttpURLConnectionGetDataTask ().execute("http://172.17.8.100/mobile/exam/findCommodityList");
}
/**
* 设置全屏
*/
private void setFullScreen() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
class HttpURLConnectionGetDataTask extends AsyncTask<String, Void, Bean> {
@Override
protected Bean doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
//判断状态码
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
String json = CharStreams.toString(new InputStreamReader(connection.getInputStream(), "UTF-8"));
Gson gson = new Gson();
return gson.fromJson(json, Bean.class);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bean bean) {
pics = bean.getResult ().getPics ();
for (int i = 0;i< pics.size (); i++){
ImageView imageView = new ImageView (TwoActivity.this);
imageView.setScaleType (ImageView.ScaleType.FIT_XY);
Picasso.with (TwoActivity.this)
.load (pics.get (i).getPic ())
.into (imageView);
final String url = pics.get (i).getUrl ();
//轮播图点击跳转
imageView.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
Intent intent = new Intent (TwoActivity.this,WebActivity.class);
intent.putExtra ("web_url",url);
startActivity (intent);
}
});
pagerimage.add (imageView);
//添加小圆点
ImageView point = new ImageView (TwoActivity.this);
if (i == 0){
point.setImageResource (R.drawable.point_green);
}else {
point.setImageResource (R.drawable.point_red);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (20,20);
params.rightMargin = 10;
llPoints.addView (point,params);
points.add (point);
}
viewPager.setAdapter (new PicAdapter (pagerimage,points));
startBanner();
product = bean.getResult().getProduct();
adapter = new MyAdapter(TwoActivity.this, product);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged ();
}
}
private List<ImageView> pagerimage = new ArrayList();
private List<ImageView> points = new ArrayList<>();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (handler.hasMessages(CODE_BANNER)){
handler.removeMessages(CODE_BANNER);
}
switch (msg.what){
case CODE_BANNER:
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
sendEmptyMessageDelayed(CODE_BANNER,DELAY);
break;
}
}
};
}
//webactivity
public class WebActivity extends AppCompatActivity {
private WebView webView;
private Toolbar toolbar;
private ImageView back;
@Override
protected void onCreate(Bundle savedInstanceState) {
setFullScreen();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
webView = findViewById(R.id.webview);//获取资源ID
toolbar = findViewById (R.id.tool_bbb);
back = findViewById (R.id.baa);
back.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
finish ();
}
});
String web_url = getIntent().getStringExtra("web_url");
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
}
});
webView.loadUrl(web_url);//得到地址里面的链接
}
/**
* 设置全屏
*/
private void setFullScreen() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
//adapter 数据的
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Bean.ResultBean.ProductBean> productBeans;
public MyAdapter(Context context, List<Bean.ResultBean.ProductBean> productBeans) {
this.context = context;
this.productBeans = productBeans;
}
@Override
public int getCount() {
return productBeans == null ? 0 : productBeans.size();
}
@Override
public Bean.ResultBean.ProductBean getItem(int position) {
return productBeans.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item, null, false);
holder.textView_name = convertView.findViewById(R.id.text_n);
holder.textView_price = convertView.findViewById(R.id.text_p);
holder.imageView_t = convertView.findViewById(R.id.image_t);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(productBeans.get(position).getImg()).into(holder.imageView_t);
holder.textView_name.setText(productBeans.get(position).getName());
holder.textView_price.setText(String.valueOf(productBeans.get(position).getMoney()));
return convertView;
}
class ViewHolder{
TextView textView_name,textView_price;
ImageView imageView_t;
}
}
//adapter 轮播图的
public class PicAdapter extends PagerAdapter {
private List<ImageView> datas;
private List<ImageView> points;
public PicAdapter(List<ImageView> datas, List<ImageView> points) {
this.datas = datas;
this.points = points;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = datas.get(position % datas.size ());
container.addView(imageView);
changePoints(position % points.size());
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
private void changePoints(int currentItem) {
for (int i = 0; i< points.size(); i++){
if (currentItem == i){
points.get(i).setImageResource(R.drawable.point_red);
}else {
points.get(i).setImageResource(R.drawable.point_green);
}
}
}
}
//mainxml的
<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.bwie.wang.activity.MainActivity">
<ImageView
android:id="@+id/im_t"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/t" />
</RelativeLayout>
//Two的
<LinearLayout 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.bwie.wang.activity.TwoActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/tool"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:contentInsetStart="0dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="首页"
android:textColor="@android:color/white"
android:textSize="22sp"
/>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/lin_Po"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@color/pointsLayoutColor"
/>
</RelativeLayout>
<GridView
android:layout_weight="8"
android:id="@+id/g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
></GridView>
</LinearLayout>
//web的
<LinearLayout 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"
android:layout_weight="1"
tools:context="com.bwie.wang.activity.WebActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bbb"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:contentInsetStart="0dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/baa"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@drawable/ic_keyboard_arrow_left_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingRight="50dp"
android:text="活动"
android:textColor="@android:color/white"
android:textSize="22sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<WebView
android:id="@+id/webview"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
//rotate
<rotate >
android:duration="800" // 设置动画持续时间
android:fromDegrees="0.0" // 设置动画开始时的角度
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50.0%" // 设置动画相对于控件的x坐标的位置
android:pivotY="50.0%" // 设置动画相对于控件的y坐标的位置
android:repeatCount="infinite" // 设置无线循环
android:toDegrees="360.0" /> // 设置动画结束时的旋转角
</rotate>