fresco和eventbus

1.fresco

//导入fresco依赖
implementation 'com.facebook.fresco:fresco:1.13.0'
//网络权限
<uses-permission android:name="android.permission.INTERNET"/>
//MyApp注册name
public class MyApp  extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化
        Fresco.initialize(this);
    }
}

//控件
<com.facebook.drawee.view.SimpleDraweeView
         android:id="@+id/sdv"
         android:layout_width="200dp"
         android:layout_height="200dp" />

public class MainActivity extends AppCompatActivity {
    private String path ="http://172.17.8.100/images/small/commodity/sjsm/sjpj/3/1.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        SimpleDraweeView sdv = findViewById(R.id.sdv);
        //网络图片解析成uri格式
        Uri uri = Uri.parse(path);
        //把图片设置给控件
        sdv.setImageURI(uri);


    }
}

2.eventbus

    依赖
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'org.greenrobot:eventbus:3.1.1'

MainActivity

//布局
  <Button
        android:id="@+id/btn_try"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn_bty"/>
     

/**
 * 接收消息的页面
 */
public class MainActivity extends AppCompatActivity {

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

        //注册EventBus
        EventBus.getDefault().register(this);

        //找控件
        Button btn_try = findViewById(R.id.btn_try);
        btn_try.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Main2Activity.class));

            }
        });
    }

   @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMsg(Msg msg){
        String msg1 = msg.getMsg();
        Toast.makeText(this, "接收消息:"+msg1, Toast.LENGTH_SHORT).show();

    }

    //解绑
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
 
 

Main2Activity

//布局
<Button
        android:id="@+id/btn_first_event"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Event"/>

**
 * 发消息页面
 */
public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //找控件
        Button btn_first_event = findViewById(R.id.btn_first_event);
        btn_first_event.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发消息
                EventBus.getDefault().post(new Msg("我使用EventBus3.0发送消息"));
                finish();
            }
        });
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值