Skip to content

Commit

Permalink
优化Handler防止内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
mCyp committed Apr 11, 2019
1 parent 86d0143 commit 822e6fd
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 弹幕图片
# BarragePhoto

- [ ] 两周内写完这个库 GO~
轻量级、多视图和防碰撞的弹幕控件
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.borient.tea.arragephotoview.data.BarrageData;
import com.bumptech.glide.Glide;
import com.orient.tea.barragephoto.adapter.AdapterListener;
import com.orient.tea.barragephoto.adapter.BarrageAdapter;
import com.orient.tea.barragephoto.ui.BarrageView;

Expand All @@ -25,7 +27,7 @@ public class MainActivity extends AppCompatActivity {
private final int ICON_RESOURCES[] = {R.drawable.icon1, R.drawable.icon2, R.drawable.icon3, R.drawable.icon4, R.drawable.icon5};

private BarrageView barrageView;
private BarrageAdapter<BarrageData,ViewHolder> mAdapter;
private BarrageAdapter<BarrageData, ViewHolder> mAdapter;
private Button mAdd;
private List<BarrageData> barrageDataList = new ArrayList<>();

Expand All @@ -37,16 +39,30 @@ protected void onCreate(Bundle savedInstanceState) {
mAdd = findViewById(R.id.btn_add);
barrageView = findViewById(R.id.barrage);
barrageView.setInterval(20);
barrageView.setDuration(10000, 2000);
barrageView.setModel(BarrageView.MODEL_COLLISION_DETECTION);
barrageView.setAdapter(mAdapter = new BarrageAdapter<BarrageData, ViewHolder>(null,this) {
barrageView.setInterceptTouchEvent(true
);
barrageView.setAdapter(mAdapter = new BarrageAdapter<BarrageData, ViewHolder>(null, this) {
@Override
public ViewHolder onCreateViewHolder(View root, int type) {
return new ViewHolder(root);
}

@Override
public int getItemLayout(BarrageData barrageData) {
return R.layout.item_barrage;
switch (barrageData.getType()) {
case 0:
return R.layout.item_barrage;
default:
return R.layout.item_barrage_bigger;
}
}
});
mAdapter.setAdapterListener(new AdapterListener<BarrageData>() {
@Override
public void onItemClick(BarrageAdapter.BarrageViewHolder<BarrageData> holder, BarrageData item) {
Toast.makeText(MainActivity.this,item.getContent()+"点击了一次",Toast.LENGTH_SHORT).show();
}
});

Expand All @@ -60,10 +76,10 @@ public void onClick(View v) {
});
}

private void initData(){
private void initData() {
int strLength = SEED.length;
for(int i = 0;i<50;i++){
mAdapter.add(new BarrageData(SEED[random.nextInt(strLength-1)],1));
for (int i = 0; i < 50; i++) {
mAdapter.add(new BarrageData(SEED[random.nextInt(strLength - 1)], random.nextInt(2)));
}
}

Expand All @@ -74,7 +90,7 @@ protected void onDestroy() {
barrageView.destroy();
}

class ViewHolder extends BarrageAdapter.BarrageViewHolder<BarrageData>{
class ViewHolder extends BarrageAdapter.BarrageViewHolder<BarrageData> {

private ImageView mHeadView;
private TextView mContent;
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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"
Expand All @@ -16,7 +16,6 @@
<com.orient.tea.barragephoto.ui.BarrageView
android:id="@+id/barrage"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="match_parent"/>

</LinearLayout>
</RelativeLayout>
26 changes: 26 additions & 0 deletions app/src/main/res/layout/item_barrage_bigger.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="@color/colorAccent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/icon1"
android:scaleType="centerCrop"
/>

<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="sssss"
android:textSize="20sp"
android:gravity="center_vertical"
/>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.orient.tea.barragephoto.model.DataSource;
import com.orient.tea.barragephoto.ui.IBarrageView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -54,7 +55,8 @@ public abstract class BarrageAdapter<T extends DataSource, VH extends BarrageAda
// 单线程的消息对立
private ExecutorService mService = Executors.newSingleThreadExecutor();
// 主线程的Handler
private Handler mHandler = new Handler(Looper.getMainLooper()){
private BarrageAdapterHandler<T> mHandler = new BarrageAdapterHandler<>(Looper.getMainLooper(),this);
/*private Handler mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Expand All @@ -73,7 +75,7 @@ public void handleMessage(Message msg) {
}
}
};
};*/


public BarrageAdapter(AdapterListener<T> adapterListener,Context context) {
Expand All @@ -83,6 +85,10 @@ public BarrageAdapter(AdapterListener<T> adapterListener,Context context) {
this.mDataList = new LinkedList<>();
}

public void setAdapterListener(AdapterListener<T> adapterListener){
this.mAdapterListener = adapterListener;
}


public void setBarrageView(IBarrageView barrageView){
this.barrageView = barrageView;
Expand Down Expand Up @@ -249,4 +255,33 @@ public void run() {
}
}
}

public static class BarrageAdapterHandler<T extends DataSource> extends Handler{
private WeakReference<BarrageAdapter> adapterReference;

public BarrageAdapterHandler(Looper looper,BarrageAdapter adapter) {
super(looper);
adapterReference = new WeakReference<BarrageAdapter>(adapter);
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

switch (msg.what){
case MSG_CREATE_VIEW:{
T data = (T)adapterReference.get().mDataList.remove();
if(data == null)
break;
if(adapterReference.get().barrageView == null)
throw new RuntimeException("please set barrageView,barrageView can't be null");
// get from cache
View cacheView = adapterReference.get().barrageView.getCacheView(data.getType());
adapterReference.get().createItemView(data,cacheView);
}
}

}

}
}
Loading

0 comments on commit 822e6fd

Please sign in to comment.