Android 处理SeekBar和滚动listview时,我的fps降至零
长期潜伏在这里,我有个问题 我制作了一个页面,显示可用歌曲列表(仍在使用选择选项),同时允许用户启动播客(最终将从服务器流式播放)并通过按钮跳到下一首和上一首歌曲 然而,当播客开始播放,seekbar/TextView开始更新fps时,fps下降到1或2左右,编舞开始告诉我跳过帧(30到120帧之间,取决于我在后台做了多少) 我研究过线程和异步任务,但我似乎不知道如何在我的系统中实现它们,所以我会保持一个不错的fps。有什么想法吗 我的主要活动代码如下:Android 处理SeekBar和滚动listview时,我的fps降至零,android,multithreading,android-asynctask,Android,Multithreading,Android Asynctask,长期潜伏在这里,我有个问题 我制作了一个页面,显示可用歌曲列表(仍在使用选择选项),同时允许用户启动播客(最终将从服务器流式播放)并通过按钮跳到下一首和上一首歌曲 然而,当播客开始播放,seekbar/TextView开始更新fps时,fps下降到1或2左右,编舞开始告诉我跳过帧(30到120帧之间,取决于我在后台做了多少) 我研究过线程和异步任务,但我似乎不知道如何在我的系统中实现它们,所以我会保持一个不错的fps。有什么想法吗 我的主要活动代码如下: import java.io.IOExc
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.example.globaldancechart.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
public class LijstScherm extends Activity {
public ImageView btnPrevious, btnPlay, btnNext;
public static SeekBar seek;
public TextView songNameBar;
public static TextView totaldurationtext, currentdurationtext;
public static int timechecker;
public static String async3 = "Updated";
int songcounter = 0;
int songplay = 0;
int songmax = 0;
int toplay = R.raw.summer;
AssetFileDescriptor afd;
public static final String[] songs = new String[] { "Comme Un Enfant",
"Burn", "Danger Zone", "Without You", "Young and Reckless",
"You Make Me Feel", "Atlas", "Antidote", "Beauty", "Event Horizon",
"Nasty Anthem", "Brutal" };
public static final String[] artists = new String[] { "Yelle", "SubVibe",
"Lana Del Rey", "KDrew", "Cobra Starship", "Balkansky",
"Swedish House Maffia", "Defeater", "Event Horizon",
"London Nebel", "Point.Blank" };
public static final long[] durations = new long[] { 0, 244000, 480000,
780000, 960000, 1140000, 1440000, 1680000, 1860000, 2040000,
2280000, 2640000 };
public static long[] async1 = new long[] { 1, 1, 1 };
Handler handler = new Handler();
static ListView listView;
public static List<ListUnit> listUnits;
final static MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lijst_scherm);
Intent intent = new Intent(LijstScherm.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
LijstScherm.this, 1, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.DAY_OF_WEEK) == java.util.Calendar.FRIDAY)
am.setRepeating(AlarmManager.RTC_WAKEUP, 82800000,
AlarmManager.INTERVAL_DAY * 7, pendingIntent);
btnPrevious = (ImageView) findViewById(R.id.btnPrevious);
btnPlay = (ImageView) findViewById(R.id.btnPlay);
btnNext = (ImageView) findViewById(R.id.btnNext);
seek = (SeekBar) findViewById(R.id.songProgress);
currentdurationtext = (TextView) findViewById(R.id.currentdurationtext);
totaldurationtext = (TextView) findViewById(R.id.totaldurationtext);
songNameBar = (TextView) findViewById(R.id.songNameBar);
afd = getApplicationContext().getResources().openRawResourceFd(
R.raw.mix);
try {
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getDeclaredLength());
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final Runnable r = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(mp.isPlaying()){
long currentPosition = mp.getCurrentPosition();
long totalDuration;
if ((currentPosition < durations[timechecker]) == true) {
if (durations[timechecker] > 0) {
totalDuration = durations[timechecker]
- durations[timechecker - 1];
currentPosition = currentPosition
- durations[timechecker - 1];
} else {
totalDuration = durations[timechecker];
}
} else {
LijstScherm.timechecker += 1;
totalDuration = durations[timechecker]
- durations[timechecker - 1];
currentPosition = currentPosition
- durations[timechecker -1];
}
currentdurationtext.setText(Utilities
.milliSecondsToTimer(currentPosition));
totaldurationtext
.setText(Utilities
.milliSecondsToTimer(totalDuration));
int progress = (int) (Utilities
.getProgressPercentage(
currentPosition,
totalDuration));
seek.setProgress(progress);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(this);
}};
mp.prepareAsync();
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
long tDuration, cDuration;
if (timechecker > 0) {
tDuration = durations[timechecker]
- durations[timechecker - 1];
cDuration = Utilities.progressToTimer(arg0.getProgress(),
tDuration);
} else {
tDuration = durations[timechecker + 1];
cDuration = Utilities.progressToTimer(arg0.getProgress(),
tDuration);
}
if (timechecker > 0)
mp.seekTo((int) (durations[timechecker - 1] + cDuration));
else
mp.seekTo((int) cDuration);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying() == false) {
mp.start();
btnPlay.setBackgroundResource(R.drawable.pause);
r.run();
} else {
mp.pause();
btnPlay.setBackgroundResource(R.drawable.play);
}
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
timechecker += 1;
mp.seekTo((int) durations[timechecker]);
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timechecker > 0) {
if (mp.getCurrentPosition() < durations[timechecker - 1] + 2000) {
mp.seekTo((int) durations[timechecker - 1]);
} else {
timechecker -= 1;
mp.seekTo((int) durations[timechecker - 2]);
}
} else {
mp.seekTo(0);
}
}
});
listUnits = new ArrayList<ListUnit>();
for (int i = 0; i < songs.length - 1; i++) {
ListUnit item = new ListUnit(songs[i], artists[i], i + 1);
listUnits.add(item);
}
listView = (ListView) findViewById(R.id.gdclijst);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.listunit, listUnits);
listView.setAdapter(adapter);
}
import java.util.List;
import com.example.globaldancechart.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomListViewAdapter extends ArrayAdapter<ListUnit> {
Context context;
public CustomListViewAdapter(Context context, int resourceId,
List<ListUnit> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
ImageView arrow;
ImageView playbutton;
TextView play;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ListUnit rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listunit, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.songname);
holder.txtDesc.setTag(position);
holder.playbutton = (ImageView) convertView.findViewById(R.id.playbutton);
holder.playbutton.setTag(position);
holder.txtTitle = (TextView) convertView.findViewById(R.id.artistname);
holder.txtTitle.setTag(position);
holder.arrow = (ImageView) convertView.findViewById(R.id.arrow);
holder.play = (TextView) convertView.findViewById(R.id.play);
holder.play.setText(String.valueOf(position + 1));
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getArtist());
holder.txtTitle.setText(rowItem.getSong());
holder.arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout rl = (RelativeLayout) v.getParent();
TextView tv = (TextView) rl.findViewById(R.id.play);
int position = Integer.parseInt(tv.getText().toString());
Context c = getContext();
Intent i = new Intent(c, InfoScherm.class);
i.putExtra("position", position);
c.startActivity(i);
}
});
holder.playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout rl = (RelativeLayout) v.getParent();
TextView tv = (TextView) rl.findViewById(R.id.play);
int position = Integer.parseInt(tv.getText().toString());
if(position > 0)
LijstScherm.timechecker = position - 1;
else
LijstScherm.timechecker = position;
}
});
return convertView;
}
}
import java.io.IOException;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.List;
导入com.example.globaldancechart.R;
导入android.app.Activity;
导入android.app.AlarmManager;
导入android.app.ListActivity;
导入android.app.pendingent;
导入android.content.Intent;
导入android.content.res.AssetFileDescriptor;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.SeekBar;
导入android.widget.TextView;
公共类LijstScherm扩展活动{
公共图像查看BTN上一页、BTN上一页、BTN下一页;
公共静态SeekBar seek;
公共文本视图歌曲名称栏;
公共静态文本查看totaldurationtext、currentdurationtext;
公共静态整数时间检测器;
公共静态字符串async3=“已更新”;
int-songcounter=0;
int-songplay=0;
int-songmax=0;
int toplay=R.raw.summer;
资产负债表;
公共静态最终字符串[]歌曲=新字符串[]{“Comme Un Enfant”,
“烧伤”、“危险地带”、“没有你”、“年轻而鲁莽”,
“你让我感觉”,“阿特拉斯”,“解药”,“美丽”,“事件地平线”,
“肮脏的颂歌”,“残忍的”};
公共静态最终字符串[]艺术家=新字符串[]{“Yelle”,“SubVibe”,
“拉娜·德雷”、“克德鲁”、“眼镜蛇星际飞船”、“巴尔干天空”,
“瑞典家庭黑手党”、“击败者”、“事件地平线”,
“伦敦内贝尔”,“直截了当”};
公共静态最终长[]持续时间=新长[]{0,244000480000,
780000, 960000, 1140000, 1440000, 1680000, 1860000, 2040000,
2280000, 2640000 };
publicstaticlong[]async1=新的long[]{1,1,1};
Handler=newhandler();
静态列表视图列表视图;
公共静态列表单位;
最终静态MediaPlayer mp=新MediaPlayer();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lijst_scherm);
意向意向=新意向(LijstScherm.this,Receiver.class);
PendingIntent PendingIntent=PendingIntent.getBroadcast(
LijstScherm.this,1,intent,0);
AlarmManager am=(AlarmManager)getSystemService(报警服务);
日历=Calendar.getInstance();
if(calendar.get(calendar.DAY\u OF_WEEK)=java.util.calendar.FRIDAY)
am.setRepeating(AlarmManager.RTC_唤醒,82800000,
AlarmManager.INTERVAL_DAY*7,待定);
btnPrevious=(ImageView)findViewById(R.id.btnPrevious);
btnPlay=(ImageView)findViewById(R.id.btnPlay);
btnNext=(ImageView)findViewById(R.id.btnNext);
seek=(SeekBar)findviewbyd(R.id.songProgress);
currentdurationtext=(TextView)findViewById(R.id.currentdurationtext);
totaldurationtext=(TextView)findViewById(R.id.totaldurationtext);
songNameBar=(TextView)findViewById(R.id.songNameBar);
afd=getApplicationContext().getResources().openRawResourceFd(
R.生拌);
试一试{
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),
afd.getDeclaredLength());
}捕获(IllegalArgumentException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(非法状态例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
最终可运行r=新可运行(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
if(mp.isPlaying()){
长currentPosition=mp.getCurrentPosition();
总持续时间长;
if((当前位置<持续时间[timechecker])==true){
if(持续时间[timechecker]>0){
totalDuration=持续时间[timechecker]
-持续时间[timechecker-1];
当前位置=当前位置
-持续时间[timechecker-1];
}否则{
totalDuration=持续时间[timechecker];
}
}否则{
LijstScherm.timechecker+=1;
totalDuration=持续时间[timechecker]
-持续时间[timechecker-1];
当前位置=当前位置
-持续时间[timechecker-1];
}
currentdurationtext.setText(实用程序
.毫秒计时器(当前位置));
全杜拉特