动画+Retrofit的使用+Frasco的使用+Butterknife的使用

MainActivity

  

package bw.com.bw_goover_01;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
}

    布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="bw.com.bw_goover_01.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

FrameActivity

 

package bw.com.bw_goover_01.demo01;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import bw.com.bw_goover_01.R;

public class FrameActivity extends AppCompatActivity {

    private ImageView mIv;
    private AnimationDrawable drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);

        mIv = (ImageView) findViewById(R.id.iv_id);

        //1, 声明动画管理器对象
        drawable  = new AnimationDrawable();

        //2, 向动画管理器中添加动画
        drawable.addFrame(getResources().getDrawable(R.mipmap.loading0001),500);
        drawable.addFrame(getResources().getDrawable(R.mipmap.loading0002),500);
        drawable.addFrame(getResources().getDrawable(R.mipmap.loading0003),500);
        drawable.addFrame(getResources().getDrawable(R.mipmap.loading0004),500);
        drawable.addFrame(getResources().getDrawable(R.mipmap.loading0005),500);

        //3, 为控件设置动画资源
        mIv.setImageDrawable(drawable);



       // 5, 默认动画执行一次
        drawable.setOneShot(false);//true 执行一次, false 执行多次
    }


    public void start(View view) {
        //4, 启动动画
        drawable.start();
    }

    public void stop(View view) {

        //4, 停止动画
        drawable.stop();
    }
}

    布局

   

<?xml version="1.0" encoding="utf-8"?>
<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="bw.com.bw_goover_01.demo01.FrameActivity">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/iv_id"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动动画"
        android:onClick="start"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止动画"
        android:onClick="stop"
        />

</LinearLayout>

   TweenActivity

  

package bw.com.bw_goover_01.demo01;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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.view.animation.TranslateAnimation;
import android.widget.ImageView;

import bw.com.bw_goover_01.R;
/**
 * 代码创建补间动画
 */
public class TweenActivity extends AppCompatActivity {

    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_treen);

        mIv = (ImageView) findViewById(R.id.iv_id);
    }

    //渐变
    public void alpha(View view) {
        //TODO 1, 申明动画资源
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
        //TODO 2, 设置持续时间
        alphaAnimation.setDuration(2000);
        //TODO 3, 启动动画
        mIv.startAnimation(alphaAnimation);
    }

    //缩放
    public void scale(View view) {
        //TODO 1, 申明动画资源
       // ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f);//左上角为参照
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//以中心的为参照
        //TODO 2, 设置持续时间
       scaleAnimation.setDuration(2000);
        //TODO 3, 启动动画
        mIv.startAnimation(scaleAnimation);
    }
    //旋转
    public void rotate(View view) {
        //TODO 1, 申明动画资源
        RotateAnimation rotateAnimation = new RotateAnimation(0f,360f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //TODO 2, 设置持续时间
        rotateAnimation.setDuration(2000);
        //TODO 3, 启动动画
        mIv.startAnimation(rotateAnimation);
    }

    //位移
    public void translate(View view) {
        //TODO 1, 申明动画资源
        TranslateAnimation translateAnimation = new TranslateAnimation(0,900,0,900);
        //TODO 2, 设置持续时间
        translateAnimation.setDuration(2000);
        //TODO 3, 启动动画
       mIv.startAnimation(translateAnimation);
    }

    //集合
    public void set(View view) {
        //TODO 1, 实例化动画的集合对象
        AnimationSet animationSet = new AnimationSet(false);

        //TODO 2, 创建每一种动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
        TranslateAnimation translateAnimation = new TranslateAnimation(0,900,0,900);

        //TODO 3, 把每个动画添加到集合中
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(translateAnimation);

        //TODO 4, 设置持续时间
        animationSet.setDuration(3000);

        //TODO 5, 为图片启动动画资源
        mIv.startAnimation(animationSet);
    }
}

    布局

  

<?xml version="1.0" encoding="utf-8"?>
<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="bw.com.bw_goover_01.demo01.TweenActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="渐变"
            android:onClick="alpha"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放"
            android:onClick="scale"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转"
            android:onClick="rotate"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="位移"
            android:onClick="translate"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="集合"
            android:onClick="set"
            />

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/iv_id"
        />

</LinearLayout>
     1, 使用Retrofit获取网络数据  
   2, 控件初始化通过ButterKnife
   3, 图片的处理使用Fresco

   4, 点击图片, 实现图片的旋转动画

   Main2Activity

   

package bw.com.bw_goover_01.demo02;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import bw.com.bw_goover_01.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
 *  1, 使用Retrofit获取网络数据
 *  2, 控件初始化通过ButterKnife
 *  3, 图片的处理使用Fresco
 *  4, 点击图片, 实现图片的旋转动画
 */
public class Main2Activity extends AppCompatActivity {


    private Unbinder unbinder;

    //TODO ButterKnife 初始化控件
    @BindView(value = R.id.rv_id)
    RecyclerView mRecycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

       //TODO 绑定ButterKnife
        unbinder = ButterKnife.bind(this);

        //TODO 设置显示的方式
        mRecycleView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        //TODO 通过Retrofit获取数据
        //  声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        // 设置基础地址
        builder.baseUrl("http://www.qubaobei.com/ios/");
        //设置Gson解析
        builder.addConverterFactory(GsonConverterFactory.create());
        // 得到Retrofit对象
        Retrofit retrofit = builder.build();
        // 得到自定义的接口
        QubaobeiInterface qubaobeiInterface = retrofit.create(QubaobeiInterface.class);
        // 调用接口中的方法, 得到Call对象
        Call<Qubaobei> call = qubaobeiInterface.getData();
        // 通过Call 中的 enqueue() 方法, 异步加载数据
        call.enqueue(new Callback<Qubaobei>() {
            @Override
            public void onResponse(Call<Qubaobei> call, Response<Qubaobei> response) {
                //TODO 获取到的数据
                Qubaobei qubaobei = response.body();

                //TODO 得到数据源
                List<Qubaobei.DataBean> data = qubaobei.getData();

                //TODO 设置适配器
                MyAdapter adapter = new MyAdapter(Main2Activity.this,data);
                mRecycleView.setAdapter(adapter);
            }
            @Override
            public void onFailure(Call<Qubaobei> call, Throwable t) {
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //TODO 解绑ButterKnife
        unbinder.unbind();
    }
}

   MyAdapter

package bw.com.bw_goover_01.demo02;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import bw.com.bw_goover_01.R;

/**
 * Created by Administrator on 2018/3/1.
 */

public  class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

    private Context context;
    private List<Qubaobei.DataBean> data;

    public MyAdapter(Context context,List<Qubaobei.DataBean> data)
    {
        this.context = context;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rv,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        holder.tv.setText(data.get(position).getTitle());

        //通过Fresco 加载图片
        holder.iv.setImageURI(Uri.parse(data.get(position).getPic()));

        //TODO 点击图片, 播放旋转动画
       holder.iv.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               RotateAnimation rotateAnimation = new RotateAnimation(0,360,
                       Animation.RELATIVE_TO_SELF,0.5f,
                       Animation.RELATIVE_TO_SELF,0.5f);
               rotateAnimation.setDuration(3000);
               holder.iv.startAnimation(rotateAnimation);
           }
       });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(value = R.id.iv_id)
        SimpleDraweeView iv;
        @BindView(value =  R.id.tv_id)
        TextView tv;
        public ViewHolder(View itemView) {
            super(itemView);
            //TODO 绑定ButterKnife
            ButterKnife.bind(this,itemView);
        }
    }
}

   MyApp

    

package bw.com.bw_goover_01.demo02;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * Created by Administrator on 2018/3/1.
 */

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        //Fresco的初始化
        Fresco.initialize(this);
    }
}

    QubaobeiInterface

   

package bw.com.bw_goover_01.demo02;

import retrofit2.Call;
import retrofit2.http.GET;

/**
 * 定义的接口
 */

public interface QubaobeiInterface {
    //http://www.qubaobei.com/ios/ --- cf/dish_list.php?stage_id=1&limit=20&page=1
    @GET("cf/dish_list.php?stage_id=1&limit=20&page=1")
    Call<Qubaobei> getData();

}

     Qubaobei为接口解析文件

  布局

   

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="bw.com.bw_goover_01.demo02.Main2Activity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv_id"
        />

</LinearLayout>

   子布局 item_rv

      

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    >

    <!--
    fresco:placeholderImage="@mipmap/ic_launcher"  占位图片
    fresco:roundAsCircle="true"  圆形图片
    圆角图片
     fresco:roundedCornerRadius="20dp"
     fresco:roundTopLeft="true"
     fresco:roundTopRight="true"
     fresco:roundBottomLeft="true"
      fresco:roundBottomRight="true"
    -->
    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/iv_id"
        fresco:placeholderImage="@mipmap/ic_launcher"
        fresco:roundAsCircle="true"
        />

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="26sp"
        android:layout_toRightOf="@id/iv_id"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="30dp"

        />

</RelativeLayout>

   依赖

compile 'com.jakewharton:butterknife:8.8.1'
    compile 'com.jakewharton:butterknife-compiler:8.8.1'
    compile 'com.facebook.fresco:fresco:1.5.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

   权限

   

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bw.com.bw_goover_01">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:name=".demo02.MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" />
        <activity android:name=".demo01.FrameActivity"></activity>
        <activity android:name=".demo01.TweenActivity">

        </activity>
        <activity android:name=".demo02.Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值