|
![](/images/resource/spacer.gif) |
|
Android ทำ custom listview ใส่รูปขนนาด Full HD แล้วกระตุก ช่วยแนะนำหน่อยครับ |
|
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ตามหัวข้อเลยครับ ใส่รูปเล็กๆ เวลา scroll เลื่อนมันไม่กระตุกน่ะ พอใส่รูปใหญ่ปุ๊บกระตุกเลย ทั้งๆ ที่แค่เปลี่ยนรูปเฉยๆ
ผมลองศึกษาจากหลายๆตัวอย่าง ส่วนมากจะเป็น custom listview แบบรูปเดียวเลยทำให้ไม่กระตุก พอเป็น custom listview รูปแต่ละ listview ไม่ซ้ำกัน และรูปมีขนาดใหญ่ มันกระตุกรัวๆเลย
อันนี้โค้ดครับ
Adapter
Code (Android-Java)
public class custom_adapter extends BaseAdapter{
public Context mContext;
public LayoutInflater mInflater;
public int[] resId;
public custom_adapter(Context context, int[] resId){
mContext = context;
mInflater = LayoutInflater.from(mContext);
this.resId = resId;
}
@Override
public int getCount() {
return resId.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
// load layout
convertView = mInflater.inflate(R.layout.listview_adap, null);
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image = (ImageView) convertView.findViewById(R.id.imgView);
holder.image.setImageResource(resId[position]);
holder.txt1.setText(String.valueOf(position) + " : First");
return convertView;
}
}
public class ViewHolder{
TextView txt1;
ImageView image;
}
MainActivity
Code (Android-Java)
public class MainActivity extends AppCompatActivity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] resId = {R.drawable.batman, R.drawable.ironman, R.drawable.spiderman, R.drawable.superman};
listview = (ListView) findViewById(R.id.listview_main);
listview.setAdapter(new custom_adapter(getApplicationContext(), resId));
}
}
ไฟล์ activity_main.xml
Code (XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.first_home.myapplication.MainActivity">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="300px"
android:src="@mipmap/ic_launcher"/>
<ListView
android:id="@+id/listview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
>
</ListView>
</RelativeLayout>
อันนี้ listview_adap.xml (จะแสดงอะไรในหน้า Listview บ้าง)
Code (XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text1"
android:text="hello"
android:textSize="20dp"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000000"
android:text="Description..."
android:layout_below="@+id/text1"/>
<ImageView
android:id="@+id/imgView"
android:layout_width="400px"
android:layout_height="400px"
android:src="@drawable/menu01"
android:layout_below="@+id/text2"/>
</RelativeLayout>
</LinearLayout>
ตอนใช้ Header ด้านบนเป็นรูปหุ่น android จากไฟล์ mipmap/ic_launcher(มีอยู่แล้วในโปรแกรม) เวลา scroll ขึ้น ลง สมูทมาก
![ic_launcher ic_launcher](https://www.thaicreate.com/upload/stock/20160410105438.png?v=1001)
หลังจากนั้นพอเปลี่ยนภาพ header จากรุป android เป็นอีกรูป(รูปแบทแมน) ซึ่งมีขนาดจริงเท่ากับ 1920x1080 เวลา scroll ขึ้น ลง กระตุกรัวๆ เลย แล้วตอนกระตุกมี มันแแจ้งเตือนดังนี้
Skipped 57 frames! The application may be doing too much work on its main thread.
![batman1 batman1](https://www.thaicreate.com/upload/stock/20160410105452.png?v=1001)
ภาพอื่นๆ ใน listview เช่นกัน เป็นภาพขนาดและ resolution เล็กๆ แบบพวก hero marvel ที่ผมใส่ใน Listview นั้นเวลา scroll ก็ไม่กระตุกเช่นกัน แต่พอเปลี่ยนเป็นภาพอื่นๆเช่นขนาด 1920x1080 แล้ว พบว่ากระตุกรัวๆ
ผมไม่แน่ใจว่าขนาด resolution หรือขนาดของไฟล์มีผลทำให้กระตุกหรือเปล่า รบกวนแนะนำทีครับ หรือมีวิธีอื่นๆที่ดีกว่า custom listview ก็แนะนำได้เช่นกัน
Tag : Mobile, Android, Mobile
![](/images/adv.jpg?v=1001)
|
ประวัติการแก้ไข 2016-04-10 11:00:33 2016-04-10 16:47:51 2016-04-10 16:48:21
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2016-04-10 10:59:28 |
By :
zixzack |
View :
1310 |
Reply :
1 |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ใชพวก Thumbnail ก่อนครับ ตอนที่จะแสดงผลค่อยไปดู Full Image มาแสดงครับ ![](/images/bbcode/grin.gif?v=1001)
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2016-04-11 11:34:20 |
By :
mr.win |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
|
|
|
![](/images/digitalocean-banner.jpg)
|
Load balance : Server 03
|