当前位置: 编程技术>移动开发
本页文章导读:
▪Gallery无限定时循环拖动成效 Gallery无限定时循环拖动效果
业务需要,需要在Gallery中实现定时循环滚动效果,而网上的大部分资料都是循环跳动,没有像屏幕拖动那种效果,经过几天的研究和资料整合,找出了解决方法.........
▪ Gallery异步加载图片跟listView动态刷新 Gallery异步加载图片和listView动态刷新
附件为eclipse项目,导入既可运行主要代码:
类1:
public class InternetGalleryActivity extends Activity implements
OnClickListener, OnFocusChangeListener {
private Gallery myGaller.........
▪ Titanium兑现Menu键相关菜单实现 Titanium实现Menu键相关菜单实现
Titanium代码实现如下:
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var activity = Ti.Android.c.........
[1]Gallery无限定时循环拖动成效
来源: 互联网 发布时间: 2014-02-18
Gallery无限定时循环拖动效果
业务需要,需要在Gallery中实现定时循环滚动效果,而网上的大部分资料都是循环跳动,没有像屏幕拖动那种效果,经过几天的研究和资料整合,找出了解决方法。
首先定时循环:
ScheduledThreadPoolExecutor timer = (ScheduledThreadPoolExecutor) Executors
.newScheduledThreadPool(1);
ImageTask imageTask = new ImageTask();
timer.scheduleAtFixedRate(imageTask, 0, 8, TimeUnit.SECONDS);
private class ImageTask extends TimerTask {
public void run() {
// getPos是我在imageAdapter中加入的方法,在ImageAdapter加一个成员变量position,然后在getView中将position
的值负值给成员变量,getPos()返回这个成员变量值 int curPos = imageAdapter.getPos();
Message message2 = new Message();
Bundle data = new Bundle();
data.putInt("pos", curPos);
message2.what = 1;
message2.setData(data);
myHandler.sendMessage(message2);
}
然后在Handler对象中处理:
imageAdapter.setCount(focusList.size());
focusGallery.setAdapter(imageAdapter);
//注意,这两个事件是我模拟处理的,其中的数字是我用用手拖动然后用在实际Gallery的onFling事件Log打印出来的。
想实现定时有一个假象的拖动动作,就模拟了这两个事件。
MotionEvent e1 = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 322.25406f, 108.34271f, 0); MotionEvent e2 = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, 146.52338f, 122.55939f, 0);
//这里一定要使用setSelection,否则每次都会从第一张图片滚动,也就是说只要速度正常,只出现第一张和第二张图片
选择当前图片,然后在当前位置下滚动。
focusGallery.setSelection(msg.getData().getInt("pos"));
if(firstLoad){
firstLoad = false;
}else{
//-945这个值可以自己测试做相应的调整,负值表示向右滑动 focusGallery.onFling(e1, e2, -945, 0); } addHeaderPointImage(focusList.size()); }
然后在Gallery的Adapter中然getCount()返回Integer.MAX_VALUE;在getView中position参数进行如下处理
int imageSize = focusList.size();
position = position % imageSize;
然后就可以无限循环了。
最后处理一下那个圆点。
focusGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> paramAdapterView,
View paramView, int paramInt, long paramLong) {
int curPos = imageAdapter.getPos();
paramInt = curPos % focusList.size();
Log.v("liuyx", curPos+"");
for (int i = 0; i < pointImageList.size(); i++) {
if (paramInt == i) {
pointImageList.get(i).setImageResource(
R.drawable.information_focus_cirlce_current);
} else {
pointImageList.get(i).setImageResource(
R.drawable.information_focus_cirlce);
}
}
}
public void onNothingSelected(AdapterView<?> paramAdapterView) {
}
});
1 楼
xiaowangzaixian
2012-05-16
有效果图吗,lz
2 楼
lyx2007825
2012-05-17
我是在原先公司的项目里改的,你可以用个Gallery试试
[2] Gallery异步加载图片跟listView动态刷新
来源: 互联网 发布时间: 2014-02-18
Gallery异步加载图片和listView动态刷新
附件为eclipse项目,导入既可运行
主要代码:
附件为eclipse项目,导入既可运行
主要代码:
类1:
public class InternetGalleryActivity extends Activity implements
OnClickListener, OnFocusChangeListener {
private Gallery myGallery;
InternetGalleryAdapter adapter;
Button bt, bt2;
// 只需要关心传入的图片地址
private String[] imageURL = new String[] {
"http://www.baidu.com/img/baidu_sylogo1.gif",
"http://misc.360buyimg.com/lib/img/e/logo.png",
"http://10.20.30.53:80/pic/cjk.jpg",
"http://10.20.30.53:80/pic/cjk2.jpg",
"http://img3.cache.netease.com/www/logo/logo_png.png", };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.main_bt);
bt.setOnClickListener(this);
bt2 = (Button) findViewById(R.id.main_bt2);
bt2.setOnClickListener(this);
myGallery = (Gallery) findViewById(R.id.main_gallery);
adapter = new InternetGalleryAdapter(this, imageURL);
myGallery.setAdapter(adapter);
// 当焦点发生改变时加载后面的图片
myGallery.setOnFocusChangeListener(this);
}
/**
* 刷新相簿
*/
public void updateGallery() {
if (null != adapter) {
Log.d("lg", "notifyDataSetChanged");
adapter.notifyDataSetChanged();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_bt:
updateGallery();
break;
case R.id.main_bt2:
// 异步listView加载
Intent intent = new Intent();
intent.setClass(this, ListViewLoadActivity.class);
startActivity(intent);
break;
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
adapter:
public class InternetGalleryAdapter extends BaseAdapter {
private Context context;
private String[] imageURL;
// private int mGalleryItemBackground;
private Bitmap[] Bitmaps;
public InternetGalleryAdapter(Context c, String[] imageURL) {
Log.d("lg", "InternetGalleryAdapter");
this.context = c;
this.imageURL = imageURL;
Bitmaps = new Bitmap[imageURL.length];
for (int i = 0; i < imageURL.length; i++) {
Resources res = context.getResources();
Bitmaps[i] = BitmapFactory.decodeResource(res,
R.drawable.ic_launcher);
}
// TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery);
/* 取得Gallery属性的Index id */
// mGalleryItemBackground =
// a.getResourceId(R.styleable.Gallery_android_galleryItemBackground,
// 0);
// 让对象的styleable属性能够反复使用
// a.recycle();
PicLoadTask picLoadTask = new PicLoadTask();
picLoadTask.execute();
}
@Override
public int getCount() {
return imageURL.length;
}
@Override
public Object getItem(int position) {
return Bitmaps[position];
}
@Override
public long getItemId(int position) {
return position;
}
public float getScale(boolean focused, int offset) {
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("lg", "getView");
ImageView imageView = new ImageView(context);
imageView.setImageBitmap(Bitmaps[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(100,
LayoutParams.FILL_PARENT));
imageView.setPadding(0, 0, 0, 0);
// imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
// 很费时的io操作,用异步线程处理
class PicLoadTask extends AsyncTask<String, Integer, String> {
// String... 可变长的输入参数,与AsyncTask.exucute()对应
protected String doInBackground(String... params) {
// 这里采用一次性全部记载的方法,适合少量图片
for (int i = 0; i < imageURL.length; i++) {
try {
// 从网络获取图片
URL aryURI = new URL(/blog_article/imageURL[i]/index.html);
URLConnection conn = aryURI.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
Bitmaps[i] = bm;
cwjHandler.post(mUpdateResults); // 发布消息让主线程接收,实现异步线程和主线程的通信
// notifyDataSetChanged(); //不能直接调用ui操作,这样不是线程安全的
is.close();
Thread.sleep(1000); // 模拟延时
} catch (Exception e) {
// 处理异常,图片加载失败
Log.d("lg", e + "");
}
}
return null;
}
}
final Handler cwjHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
notifyDataSetChanged(); // 不能直接在AsyncTask中调用,因为不是线程安全的
}
};
}
类2:listViewLoad
public class ListViewLoadActivity extends ListActivity implements
OnScrollListener {
private LinearLayout mLoadLayout;
private ListView mListView;
private ListViewAdapter mListViewAdapter = new ListViewAdapter();
private int mLastItem = 0;
private int mCount = 41;
private final Handler mHandler = new Handler();
private final LayoutParams mProgressBarLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
private final LayoutParams mTipContentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* "加载项"布局,此布局被添加到ListView的Footer中。
*/
mLoadLayout = new LinearLayout(this);
mLoadLayout.setMinimumHeight(60);
mLoadLayout.setGravity(Gravity.CENTER);
mLoadLayout.setOrientation(LinearLayout.HORIZONTAL);
/**
* 向"加载项"布局中添加一个圆型进度条。
*/
ProgressBar mProgressBar = new ProgressBar(this);
mProgressBar.setPadding(0, 0, 15, 0);
mLoadLayout.addView(mProgressBar, mProgressBarLayoutParams);
/**
* 向"加载项"布局中添加提示信息。
*/
TextView mTipContent = new TextView(this);
mTipContent.setText("加载中...");
mLoadLayout.addView(mTipContent, mTipContentLayoutParams);
/**
* 获取ListView组件,并将"加载项"布局添加到ListView组件的Footer中。
*/
mListView = getListView();
mListView.addFooterView(mLoadLayout);
/**
* 组ListView组件设置Adapter,并设置滑动监听事件。
*/
setListAdapter(mListViewAdapter);
mListView.setOnScrollListener(this);
}
public void onScroll(AbsListView view, int mFirstVisibleItem,
int mVisibleItemCount, int mTotalItemCount) {
mLastItem = mFirstVisibleItem + mVisibleItemCount - 1;
if (mListViewAdapter.count > mCount) {
mListView.removeFooterView(mLoadLayout);
}
}
public void onScrollStateChanged(AbsListView view, int mScrollState) {
/**
* 当ListView滑动到最后一条记录时这时,我们会看到已经被添加到ListView的"加载项"布局, 这时应该加载剩余数据。
*/
if (mLastItem == mListViewAdapter.count
&& mScrollState == OnScrollListener.SCROLL_STATE_IDLE) {
if (mListViewAdapter.count <= mCount) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mListViewAdapter.count += 10;
mListViewAdapter.notifyDataSetChanged();
mListView.setSelection(mLastItem);
}
}, 1000);
}
}
}
class ListViewAdapter extends BaseAdapter {
int count = 10;
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
TextView mTextView;
if (view == null) {
mTextView = new TextView(ListViewLoadActivity.this);
} else {
mTextView = (TextView) view;
}
mTextView.setText("Item " + position);
mTextView.setTextSize(20f);
mTextView.setGravity(Gravity.CENTER);
mTextView.setHeight(60);
return mTextView;
}
}
}
mainlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Gallery
android:id="@+id/main_gallery"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#55000000"
android:spacing="1dp" />
<Button
android:id="@+id/main_bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" >
</Button>
<Button
android:id="@+id/main_bt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" >
</Button>
</LinearLayout>
[3] Titanium兑现Menu键相关菜单实现
来源: 互联网 发布时间: 2014-02-18
Titanium实现Menu键相关菜单实现
Titanium代码实现如下:
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var activity = Ti.Android.currentActivity;
//
// create base UI tab and root window
//
var win = Titanium.UI.createWindow({
title:'Titanium Android ContextMenu使用'
});
win.backgroundColor = 'white';
var b1 = Ti.UI.createButton({
title : '打开窗体',
height : 'auto',
width : 'auto'
});
// Here is an example of creating the menu handlers after window creation but before open.
b1.addEventListener('click', function(e) {
var w = Ti.UI.createWindow({
backgroundColor : 'blue',
navBarHidden : false
});
w.activity.onCreateOptionsMenu = function(e) {
var menu = e.menu;
var m1 = menu.add({ title : '关闭窗体' });
m1.addEventListener('click', function(e) {
w.close();
});
};
w.activity.onPrepareOptionsMenu = function(e) {
var menu = e.menu;
var mi = menu.findItem(2);
if (mi == null) {
mi = menu.add({
itemId : 2,
order : 1,
title : '加载资源'
});
mi.addEventListener('click', function(e) {
Ti.UI.createNotification({ message : "To you and yours." }).show();
});
}
};
var l = Ti.UI.createLabel({
backgroundColor : 'white', color : 'black',
width : 'auto', height : 'auto',
text : '请点击Menu菜单按钮,窗体关闭'
});
w.add(l);
w.open({ animated : true});
});
win.add(b1);
win.open();
最新技术文章: