豆瓣客户端的实现07

需求分析

解析服务端HTML数据 以ListView数据显示出来 目前图片显示还有问题 需要改进



上代码分析
MainActivity.java

public class MainActivity extends Activity {

	private ListView subjectlist;
	Map<String, SoftReference<Bitmap>> iconCache;
	List<NewBook> newbooks;
	NewBookAdapter adapter;
	
	public TextView mTextViewTitle;
	public RelativeLayout mRelativeLoading;
	
	public ImageButton mImageBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.e("MainActivity", "onCreate");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subject);
		setupView();
		setListener() ;
		fillData();
	}
	
	public void showLoading() {
		mRelativeLoading.setVisibility(View.VISIBLE);
		AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
		aa.setDuration(1000);
		ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
		sa.setDuration(1000);
		AnimationSet set = new AnimationSet(false);
		set.addAnimation(sa);
		set.addAnimation(aa);
		mRelativeLoading.setAnimation(set);
		mRelativeLoading.startAnimation(set);
	}

	public void hideLoading() {
		AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
		aa.setDuration(1000);
		ScaleAnimation sa = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
		sa.setDuration(1000);
		AnimationSet set = new AnimationSet(false);
		set.addAnimation(sa);
		set.addAnimation(aa);
		mRelativeLoading.setAnimation(set);
		mRelativeLoading.startAnimation(set);
		mRelativeLoading.setVisibility(View.INVISIBLE);
	}

	public void showToast(String text) {
		Toast.makeText(this, text, 0).show();
	}

	
	public void setupView() {
		Log.e("MainActivity", "setupView");
		mRelativeLoading = (RelativeLayout) this.findViewById(R.id.loading);
		subjectlist = (ListView) this.findViewById(R.id.subjectlist);

	}


	public void setListener() {
		Log.e("MainActivity", "setListener");
	}

	
	public void fillData() {
		Log.e("MainActivity", "fillData");
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {

				try {
					newbooks = NetUtil.getNewBooks(getApplicationContext());
					return true;
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}

			}

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				showLoading();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				hideLoading();
				super.onPostExecute(result);
				if(result){
					//设置数据适配器 
					if(adapter==null){
						adapter = new NewBookAdapter();
						subjectlist.setAdapter(adapter);
					}else{
						adapter.notifyDataSetChanged();
					}
				}else{
					showToast("数据获取 失败,请检查网络");
				}
			}

		}.execute();

	}

	private class NewBookAdapter extends BaseAdapter {

		public int getCount() {
			Log.e("MainActivity", "getCount");
			return newbooks.size();
		}

		public Object getItem(int position) {
			Log.e("MainActivity", "getItem");
			return newbooks.get(position);
		}

		public long getItemId(int position) {
			Log.e("MainActivity", "getItemId");
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Log.e("MainActivity", "getView");
			NewBook newbook = newbooks.get(position);
			View view;
			if(convertView==null){
				view = View.inflate(MainActivity.this, R.layout.new_book_item, null);
			}else{
				view = convertView;
			}
			final ImageView iv = new ImageView(MainActivity.this);
			
		
			LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_book_image);
			//清空ll的里面的view对象
			ll.removeAllViews();
			
			ll.addView(iv, new LayoutParams(60, 60));
			
			TextView tv_title = (TextView) view.findViewById(R.id.book_title);
			TextView tv_description = (TextView) view
					.findViewById(R.id.book_description);
			
			
			tv_title.setText(newbook.getName());
			tv_description.setText(newbook.getDescription());
			SharedPreferences  sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
			boolean canloadicon = sp.getBoolean("canloadicon", false);
			if(canloadicon){
			LoadImageAsynTask task = new LoadImageAsynTask(new LoadImageAsynTaskCallback() {
				
				public void beforeLoadImage() {
					iv.setImageResource(R.drawable.book);
				}
				
				public void afterLoadImage(Bitmap bitmap) {
					if(bitmap!=null){
						iv.setImageBitmap(bitmap);
					}else{
						iv.setImageResource(R.drawable.book);
					}
					
				}
			});
			task.execute(newbook.getIconpath());
			}else{
				iv.setImageResource(R.drawable.book);
			}
			return view;
		}

	}

	
	
	
}

subject.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_books"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF5F5F5"
    android:orientation="vertical"
    android:padding="0px" >

   

    <RelativeLayout
        android:id="@+id/mainRL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0" >

        <ListView
            android:id="@+id/subjectlist"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5.0dip"
            android:layout_marginRight="3.0dip"
            android:layout_weight="1.0"
            android:cacheColorHint="#00000000"
            android:divider="@android:color/transparent"
            android:dividerHeight="5.0dip"
            android:listSelector="@android:color/transparent"
            android:paddingTop="5.0dip"
            android:scrollbarStyle="outsideInset" >
        </ListView>

        <RelativeLayout
            android:id="@+id/loading"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            
            android:visibility="invisible" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#AA111111"
                android:gravity="center"
                android:padding="8dip" >

                <ProgressBar
                    android:id="@+id/progress"
                    android:layout_width="24dip"
                    android:layout_height="24dip"
                    android:layout_marginBottom="6dip"
                    android:layout_marginTop="6dip"
                    android:indeterminate="true" />

                <TextView
                    android:id="@+id/txt_loading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="8dip"
                    android:layout_toRightOf="@id/progress"
                    android:shadowColor="#000000"
                    android:shadowDx="0"
                    android:shadowDy=".5"
                    android:shadowRadius="1"
                    android:text="正在 获取数据"
                    android:textColor="#FFFFFF" />
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

new_book_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/category_selector"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/ll_book_image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />
           
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingLeft="15dip" >

            <TextView
                android:id="@+id/book_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="50dip"
                android:textColor="@android:color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/book_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="60dip"
                android:textColor="@android:color/black"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

NewBook.java 数据类

public class NewBook {

	private String name; //书名
	private String description; //描述
	private String summary; //简介
	private String iconpath; //图片地址
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getIconpath() {
		return iconpath;
	}
	public void setIconpath(String iconpath) {
		this.iconpath = iconpath;
	}
	
}

NetUtil.java 网络工具类

public class NetUtil {

	

	public static Bitmap getImage(String path) throws Exception {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		InputStream is = conn.getInputStream();
		return BitmapFactory.decodeStream(is);
	}

	
	
	public static List<NewBook> getNewBooks(Context context) throws Exception {
		String path = context.getResources().getString(R.string.newbookpath);
		URL url = new URL(path);
		URLConnection conn = url.openConnection();
		Source source = new Source(conn);
		List<NewBook> newbooks = new ArrayList<NewBook>();
		List<Element> lielements = source.getAllElements("li");
		System.out.println(lielements.size());
		for (Element lielement : lielements) {
			List<Element> childrenlists = lielement.getChildElements();
			if (childrenlists.size() == 2) {

				if ("detail-frame".equals(childrenlists.get(0)
						.getAttributeValue("class"))) {
					NewBook newbook = new NewBook();
					// 数目对应的div信息
					Element div = childrenlists.get(0);
					List<Element> divChildren = div.getChildElements();
					String name = divChildren.get(0).getTextExtractor()
							.toString();
					newbook.setName(name);
					String description = divChildren.get(1).getTextExtractor()
							.toString();
					newbook.setDescription(description);
					String summary = divChildren.get(2).getTextExtractor()
							.toString();
					newbook.setSummary(summary);

					Element achild = childrenlists.get(1);
					String iconpath = achild.getChildElements().get(0)
							.getAttributeValue("src");
					newbook.setIconpath(iconpath);
					newbooks.add(newbook);
				}

			}

		}
		return newbooks;
	}

}

LoadImageAsynTask.java 异步加载图片工具类

public class LoadImageAsynTask extends AsyncTask<String, Void, Bitmap> {
	LoadImageAsynTaskCallback loadImageAsynTaskCallback;
	
	
	
	public LoadImageAsynTask(LoadImageAsynTaskCallback loadImageAsynTaskCallback) {
		this.loadImageAsynTaskCallback = loadImageAsynTaskCallback;
	}

	public interface LoadImageAsynTaskCallback{
	   public void	beforeLoadImage();
	   public void afterLoadImage(Bitmap bitmap);
	}

	/**
	 * 当异步任务执行之前调用
	 */
	@Override
	protected void onPreExecute() {
		//初始化的操作具体怎么去实现, LoadImageAsynTask 不知道
		// 需要让调用这个 LoadImageAsynTask 的人 去实现 
		loadImageAsynTaskCallback.beforeLoadImage();
		super.onPreExecute();
	}

	/**
	 * 异步任务执行之后调用
	 */
	@Override
	protected void onPostExecute(Bitmap result) {
		loadImageAsynTaskCallback.afterLoadImage(result);
		super.onPostExecute(result);
	}

	/**
	 * 后台子线程运行的异步任务 
	 * String... params 可变长度的参数 
	 */
	@Override
	protected Bitmap doInBackground(String... params) {
		try {
			String path = params[0];
			URL url = new URL(path);
			HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			return  BitmapFactory.decodeStream(is);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

}

 AndroidManifest.xml 加权限 <application上面

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

导入外部的Jar包


代码还有问题 明天继续搞


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值