RRMO
//主类
public class MainActivity extends AppCompatActivity { private MyCircleView my_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 my_view = findViewById(R.id.my_view); } public void onClick(View view){ my_view.setColor(Color.BLUE); } public void add(View view){ my_view.speed(); } public void slow(View view){ my_view.slowDown(); } public void pauseOrStart(View view){ my_view.pauseOrStart(); } }
//主布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/set_color_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="onClick" android:text="设置颜色" /> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/set_color_btn" android:layout_centerHorizontal="true" android:onClick="add" android:text="加速" /> <Button android:id="@+id/slow" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/add" android:layout_centerHorizontal="true" android:onClick="slow" android:text="减速" /> <Button android:id="@+id/pause_or_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/slow" android:layout_centerHorizontal="true" android:onClick="pauseOrStart" android:text="暂定/开始" /> <com.example.shexinhai_1118.MyCircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_view" android:layout_centerInParent="true" app:circlr_bound_color="@color/colorAccent" app:circlr_bound_width="3dp" /> </RelativeLayout>
//列表显示Activity
public class SecondActivity extends AppCompatActivity implements ShowView { @BindView(R.id.myrecycle) RecyclerView recycleview; private List<NewsBean.DataBean>list; private LinearLayoutManager manager; private List<String> mListImage; private Banner banner; private IAdapter adapter; private ShowPresenter presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); recycleview = findViewById(R.id.myrecycle); ButterKnife.bind(this); manager = new LinearLayoutManager(this); recycleview.setLayoutManager(manager); //p关联v presenter = new ShowPresenterImpl(this); //p关联m 做网络请求 presenter.relevance(); adapter = new IAdapter(SecondActivity.this,list); recycleview.setAdapter(adapter); adapter.setOnItemClickListener(new IAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Intent intent=new Intent(SecondActivity.this,ThreadActivity.class); startActivity(intent); } }); banner= findViewById(R.id.mybanner); //加载图片的方法 banner.setImageLoader(new GlideImageLoader()); mListImage= new ArrayList<>(); //向图片集合中添加网网址 mListImage.add("http://pic32.nipic.com/20130817/9745430_101836881000_2.jpg"); mListImage.add("http://pic15.nipic.com/20110630/6322714_105943746342_2.jpg"); mListImage.add("http://pic48.nipic.com/file/20140916/2531170_195153248000_2.jpg"); mListImage.add("http://img.taopic.com/uploads/allimg/140626/240469-1406261S24553.jpg"); mListImage.add("http://pic77.nipic.com/file/20150911/21721561_155058651000_2.jpg"); //设置Banner图片集合 banner.setImages(mListImage); //切换的时间 banner.setDelayTime(2000); //启动banner banner.start(); adapter.setOnItemClickListener(new IAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Intent intent = new Intent(SecondActivity.this,ThreadActivity.class); startActivity(intent); } }); } @Override public void showData(NewsBean news) { adapter.setData(news); } }
//列表布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.youth.banner.Banner android:id="@+id/mybanner" android:layout_width="match_parent" android:layout_height="150dp"> </com.youth.banner.Banner> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="23dp"> <ImageView android:background="@drawable/jiantou" android:layout_width="20dp" android:layout_marginLeft="7dp" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="8dp" android:layout_width="wrap_content" android:text="免费试听" android:gravity="center" android:textSize="18dp" android:textColor="#101010" android:layout_height="23dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="200dp" android:text="查看全部 >" android:gravity="center"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/myrecycle" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
//适配器
public class IAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener { private OnItemClickListener mOnItemClickListener = null; private List<NewsBean.DataBean> list; private Context context; public IAdapter(Context context,List<NewsBean.DataBean>list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.activity_item, parent, false); view.setOnClickListener(this); IViewHolder viewHolder = new IViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof IViewHolder) { IViewHolder iViewHolder = (IViewHolder) holder; LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) iViewHolder.adapterImageview.getLayoutParams(); iViewHolder.adapterImageview.setLayoutParams(params); iViewHolder.title.setText(list.get(position).getTitle()); Glide.with(context).load(list.get(position).getImage_url()).into(iViewHolder.adapterImageview); } } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public void setData(NewsBean bean){ if(this.list == null){ this.list = new ArrayList<>(); } this.list.addAll(bean.getData()); notifyDataSetChanged(); } //点击事件接口 //define interface public interface OnItemClickListener { void onItemClick(View view , int position); } public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } @Override public void onClick(View view) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取position mOnItemClickListener.onItemClick(view,(int)view.getTag()); } } static class IViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.myimage) ImageView adapterImageview; @BindView(R.id.mytext) TextView title; IViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } }
//加载图片的类
public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { //使用glide加载图片 Glide.with(context).load(path).into(imageView); } }
//自定义view到的类
public class MyCircleView extends View { //当前画笔画圆的颜色 private int CurrenCircleBoundColor; private Paint paint; ////从xml中获取的颜色 private int circleBundColor; private float circleBoundWidth; private float pivotX; private float pivotY; private float radius=130; private float currentDegree=0; private int currentSpeed=1; private boolean isPause=false; public MyCircleView(Context context) { super(context); initView(context); } public MyCircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCircleView); for (int i = 0; i < typedArray.getIndexCount(); i++) { //就是我们自定义的属性的资源id int attr = typedArray.getIndex(i); switch (attr){ case R.styleable.MyCircleView_circlr_bound_color: circleBundColor = typedArray.getColor(attr, Color.RED); CurrenCircleBoundColor=circleBundColor; break; case R.styleable.MyCircleView_circlr_bound_width: circleBoundWidth = typedArray.getDimension(attr, 3); break; } } } public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context){ paint = new Paint(); } public void setColor(int color){ if (CurrenCircleBoundColor!=color){ CurrenCircleBoundColor=color; }else { CurrenCircleBoundColor=circleBundColor; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setAntiAlias(true); paint.setColor(CurrenCircleBoundColor); paint.setStrokeWidth(circleBoundWidth); paint.setStyle(Paint.Style.STROKE); pivotX = getWidth() / 2; pivotY = getHeight() / 2; canvas.drawCircle(pivotX,pivotY,radius,paint); canvas.save(); //旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的 canvas.rotate(currentDegree,pivotX,pivotY); //提供了一些api可以用来画线(画路径) Path path = new Path(); //从哪开始画 从A开始画 path.moveTo(pivotX+radius,pivotY); //从A点画一个直线到D点 path.lineTo(pivotX+radius-20,pivotY-20); //从D点画一个直线到B点 path.lineTo(pivotX+radius,pivotY+20); //从B点画一个直线到C点 path.lineTo(pivotX+radius+20,pivotY-20); //闭合 -- 从C点画一个直线到A点 path.close(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLACK); canvas.drawPath(path,paint); canvas.restore(); //旋转的度数一个一个度数增加, 如果乘以一个速度的话,按一个速度速度增加 currentDegree+=1*currentSpeed; if (!isPause){ invalidate(); } } public void speed(){ ++currentSpeed; if (currentSpeed>=10){ currentSpeed=10; Intent intent = new Intent(getContext(),SecondActivity.class); getContext().startActivity(intent); } } public void slowDown(){ --currentSpeed; if (currentSpeed<=1){ currentSpeed=1; Toast.makeText(getContext(),"我是小蜗牛",Toast.LENGTH_SHORT).show(); } } public void pauseOrStart(){ //如果是开始状态的话去重新绘制 if (isPause){ isPause=!isPause; invalidate(); }else { isPause=!isPause; } } }
//view层
public interface ShowView { void showData(NewsBean news); }
//Presenter层
public interface ShowPresenter { void relevance(); }
public class ShowPresenterImpl implements ShowPresenter,OnFinishListener { ShowView showView; private final ShowModel showModel; //初始化 public ShowPresenterImpl(ShowView showView){ this.showView = showView; //多态 showModel = new ShowModelImpl(); } @Override public void relevance() { //p跟m关联 showModel.getData(this); } @Override public void onSuccess(NewsBean news) { //关联view showView.showData(news); } }
//modle层
public interface ShowModel { void getData(OnFinishListener listener); }
public class ShowModelImpl implements ShowModel { @Override public void getData(final OnFinishListener listener) { ApiService apiService = RetrofitUtils.getInstance().getApiService(Aapi.api_data, ApiService.class); Observable<NewsBean> observable = apiService.getData(); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<NewsBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(NewsBean bean) { listener.onSuccess(bean); } }); } }
//Api接口
public class Aapi { public static final String api_data ="http://result.eolinker.com/"; }
public interface ApiService { @GET("iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio") Observable<NewsBean> getData(); }
//依赖
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.youth.banner:banner:1.4.9' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.1' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.facebook.fresco:fresco:0.14.1' compile 'fm.jiecao:jiecaovideoplayer:5.5' compile 'com.android.support:recyclerview-v7:26.+' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.github.bumptech.glide:glide:4.0.0' testCompile 'junit:junit:4.12' // compile project(':ok')
//权限
<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
//attrs.xml文件
<resources> <declare-styleable name="MyCircleView"> <attr name="circlr_bound_width" format="dimension"></attr> <attr name="circlr_bound_color" format="color"></attr> </declare-styleable> </resources>