在我的应用程序中,我需要从服务器流式传输视频和图像。如果视频不可用,则应检索图像并将其显示在照片库中。我成功地完成了这部分,但一次只能使用一张图像。但是这种方法很慢,因为它需要很长时间才能加载下一张图像。
我想要做的是传递 4 个 url(在本示例中,4 是要从服务器检索的图像总数),但通过使用Onclicklistener
. 请有任何建议。我应该如何进行?谢谢
public class Imageviewing extends Activity implements OnGestureListener{
private static final String TAG = null;
ImageView imView;
String imageUrl="<myServerlink>";
Random r= new Random();
String urilink;
private GestureDetector myGesture ;
private static final int SWIPE_MIN_DISTANCE = 75;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageviewing);
Intent intent = getIntent();
// setContentView(R.layout.);
// String
urilink = intent.getExtras().getString("videouri");
Log.e(TAG, urilink.toString());
Button bt3= (Button)findViewById(R.id.get_imagebt);
bt3.setOnClickListener(getImgListener);
imView = (ImageView)findViewById(R.id.imview);
myGesture = new GestureDetector(this);
}
View.OnClickListener getImgListener = new View.OnClickListener()
{
@Override
public void onClick(View view) {
finish();
}
};
@Override
public boolean onTouchEvent(MotionEvent event){
return myGesture.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_imageviewing, menu);
return true;
}
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
Log.i("im url","Image to Imageview");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
try {
//do not do anything if the swipe does not reach a certain length of distance
// if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
// return false;
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
//if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int i = r.nextInt(5-1)+1;
downloadFile(imageUrl+urilink+"-"+i+".jpg");
Log.i("im url",imageUrl+urilink+"-"+i+".jpg");
return false;
}
/* // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ViewHistory();
}*/
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
}