当前位置:  编程技术>移动开发
本页文章导读:
    ▪SlidingDrawer隐藏式屉子        SlidingDrawer隐藏式抽屉   private String items[] = {"项目1" , "项目2" ,"项目3" ,"项目4" ,"项目5"}; private int icons[] = {R.drawable.img1 , R.drawable.img2 , R.drawable.img3 , R.drawable.img4, R.drawable.img5}; private SlidingDrawe.........
    ▪ 资料读写        文件读写 Android SDCard操作(文件读写,容量计算) :http://zhuyonghui116.blog.hexun.com/56778119_d.htmlAndroid中读写文件: http://blog.csdn.net/cocodehouse/article/details/5974288增加权限:<uses-permission android:name="a.........
    ▪ 关于ListView中性能优化中图片加载有关问题       关于ListView中性能优化中图片加载问题 ListView的性能优化网上很多信息,但是涉及到异步加载图片问题就会出现问题。 具体参看上篇文章http://314858770.iteye.com/admin/blogs/1217594   如果每次都重新.........

[1]SlidingDrawer隐藏式屉子
    来源: 互联网  发布时间: 2014-02-18
SlidingDrawer隐藏式抽屉

 

private String items[] = {"项目1" , "项目2" ,"项目3" ,"项目4" ,"项目5"};
	private int icons[] = {R.drawable.img1 , R.drawable.img2 , R.drawable.img3 , R.drawable.img4, R.drawable.img5};
	
	private SlidingDrawer slidingDrawer;
	private GridView gridView;
	private ImageView image;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sling_drawer);
		
		slidingDrawer = (SlidingDrawer)findViewById(R.id.slidingDrawer1); 
		gridView = (GridView)findViewById(R.id.gridView1); 
		image = (ImageView)findViewById(R.id.handle);
		
		NewSlidingDrawerDrawerAdapter adapter = new NewSlidingDrawerDrawerAdapter(this, items , icons);
		gridView.setAdapter(adapter);
		gridView.setNumColumns(3);
		
		slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener(){

			public void onDrawerOpened() {
				image.setImageResource(R.drawable.close);
			}
			
		});
		
		slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener(){

			public void onDrawerClosed() {
				image.setImageResource(R.drawable.open);
			}
			
		});
	}

	public class NewSlidingDrawerDrawerAdapter extends BaseAdapter {

		private Context context;
		private String[] items;
		private int[] icons;

		public NewSlidingDrawerDrawerAdapter(Context context ,String[] items , int[] icons) {
			this.context = context;
			this.items = items;
			this.icons = icons;
		}

		public int getCount() {
			return items.length;
		}

		public Object getItem(int position) {
			return items[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater factory = LayoutInflater.from(context);
			/* 使用grid.xml为每几个item的Layout */
			View v = (View) factory.inflate(R.layout.gridview_item, null);
			/* 取得View */
			ImageView iv = (ImageView) v.findViewById(R.id.ItemImage);
			TextView tv = (TextView) v.findViewById(R.id.ItemText);
			/* 设定显示的Image与文字 */
			iv.setImageResource(icons[position]);
			tv.setText(items[position]);
			return v;
		}

	}

 gridview_item.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:scrollbars="vertical">
	<ImageView
		android:layout_height="100dip"
		android:id="@+id/ItemImage"
		android:layout_width="80dip"
		android:src="/blog_article/@drawable/img1/index.html"
		android:layout_gravity="center_horizontal" />
	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:id="@+id/ItemText"
		android:text="名称" />
</LinearLayout>

sliding_drawer.xml

 

 

<?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:orientation="vertical">
	<SlidingDrawer
		android:layout_width="fill_parent"
		android:id="@+id/slidingDrawer1"
		android:layout_height="fill_parent"
		android:handle="@+id/handle"
		android:content="@+id/content"
		android:orientation="horizontal">
		<LinearLayout
			android:id="@+id/linearLayout1"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content">
			<ImageView
				android:layout_width="wrap_content"
				android:src="/blog_article/@drawable/open/index.html"
				android:layout_height="wrap_content"
				android:id="@+id/handle"></ImageView>
		</LinearLayout>
		<LinearLayout
			android:id="@+id/content"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent">
			<GridView
				android:layout_width="wrap_content"
				android:id="@+id/gridView1"
				android:layout_height="fill_parent"></GridView>
		</LinearLayout>
	</SlidingDrawer>
</LinearLayout>
 



    
[2] 资料读写
    来源: 互联网  发布时间: 2014-02-18
文件读写
Android SDCard操作(文件读写,容量计算) :http://zhuyonghui116.blog.hexun.com/56778119_d.html
Android中读写文件: http://blog.csdn.net/cocodehouse/article/details/5974288

增加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
String sDStateString = android.os.Environment.getExternalStorageState(); 


// 拥有可读可写权限 
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
   ... ...
}

然后用FileInputStream和FileOutputStream进行读写。


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.file.rw"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ReadWriteFilesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



<?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:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="a.txt" />



    <Button
        android:id="@+id/button1"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:text="Load" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/button2"
        android:layout_width="68dp"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>



package com.file.rw;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ReadWriteFilesActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1,button2;
	private EditText editText1,editText2;
	private TextView textview;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);
        textview =(TextView)findViewById(R.id.textview);
        //load file from sd card
        button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textview.setText("Load............");
				String sdStatus = android.os.Environment.getExternalStorageState();
				if(sdStatus.equalsIgnoreCase(android.os.Environment.MEDIA_MOUNTED)){
					textview.setText(editText1.getText());
					File rootDir = android.os.Environment.getExternalStorageDirectory();
					File readFile = new File(rootDir.getAbsolutePath()+File.separator+editText1.getText());
					if(!readFile.exists()){
						textview.setText(editText1.getText()+"不存在.");
					}else{
						try {
							FileInputStream in = new FileInputStream(readFile);
							byte[] buffer = new byte[in.available()];
							in.read(buffer);
							in.close();
							editText2.setText(new String(buffer));
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					}
				}//end of android.os.Environment.MEDIA_MOUNTED
				
			}
		});
        
        button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(editText1.getText()==null||editText1.getText().toString().trim().length()==0){
					textview.setText("第一个输入框没有输入文件名.");
					return;
				}
				if(editText2.getText()==null||editText2.getText().toString().trim().length()==0){
					textview.setText("第而个输入框没有输入文件内容.");
					return;
				}
				String sdStatus = android.os.Environment.getExternalStorageState();
				if(android.os.Environment.MEDIA_MOUNTED.equalsIgnoreCase(sdStatus)){
					File root = android.os.Environment.getExternalStorageDirectory();
					File newFile = new File(root.getAbsolutePath()+File.separator+editText1.getText());
					try {
						if(!newFile.exists()){
							newFile.createNewFile();
						}
						FileOutputStream out = new FileOutputStream(newFile);
						out.write(editText2.getText().toString().getBytes());
						out.close();
						textview.setText("写文件成功.");
					} catch (Exception e) {
						// TODO: handle exception
					}
					
				}
				
			}
		});
        
    }
}

    
[3] 关于ListView中性能优化中图片加载有关问题
    来源: 互联网  发布时间: 2014-02-18
关于ListView中性能优化中图片加载问题

ListView的性能优化网上很多信息,但是涉及到异步加载图片问题就会出现问题。

具体参看上篇文章http://314858770.iteye.com/admin/blogs/1217594

 

如果每次都重新inflate一个新的View出来肯定会造成性能损失严重,可能会出现listview滚动是很卡的情况,还会出现内存溢出。

现在想出一个方法就是每次都添加一个标识,然后设置图片的时候检查这个标识,如果不相符则忽略此次。

 

适配器中的关键代码

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
			viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
			viewHolder.textView = (TextView)convertView.findViewById(R.id.textView);
			convertView.setTag(viewHolder);
		}
		viewHolder = (ViewHolder)convertView.getTag();
		
		viewHolder.textView.setText(stringList.get(0)[0]);
		final ImageView imageView = viewHolder.imageView;
		imageView.setImageDrawable(null); //reset default
		imageView.setTag(position);
		asyncImageLoader.loadDrawable(stringList.get(0)[1], new AsyncImageLoader.ImageCallback() {
			@Override
			public void imageLoaded(Drawable imageDrawable, String imageUrl,int key) {
				if((Integer)imageView.getTag() != key) return;  //不是当前显示的View,则忽略此次
				if(imageDrawable != null){
					imageView.setImageDrawable(imageDrawable);
				}
			}
		},position);
		return convertView;
	}

 

	static class ViewHolder{
		TextView textView;
		ImageView imageView;
	}

 

异步加载图片的代码

	public Drawable loadDrawable(final String imageUrl,
			final ImageCallback callback,final int key) {

		if (!URLUtil.isValidUrl(/blog_article/imageUrl/index.html)) {
			callback.imageLoaded(null, imageUrl,key);
			return null;
		}

		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			if (softReference.get() != null) {
				Log.d(AsyncImageLoader.class.getName(),
						"imageCache has this image,use cache. the url:"
								+ imageUrl);
				callback.imageLoaded(softReference.get(), imageUrl,key);
				return softReference.get();
			}
		}
      //更多代码
	}
 

	public interface ImageCallback {
		public void imageLoaded(Drawable imageDrawable, String imageUrl,int key);
	}
 

经过此次修改,再测试时不会出现之前的内存持续增长  和 滚动很卡的情况。


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3