zoukankan      html  css  js  c++  java
  • 班级派团队项目小计(一)

         long time no see,好久没有写博客了。今天大致了解到了作业模块的实现方法,大体想以图片上传到服务器和查看的方式实现。 

         看了查看网络图片的相关视频,还没弄的太懂,想通过写博客的方式来理清脉络,让自己明白一点,顺便补充知识。

    效果截图:

    当点击go时,界面会显示EidtText中网址所对应的图片

    布局使用了ImageView来显示图片

    **************************************************************************************************

    ImageView

    首先确定下ImageView的全路径:android.widget.ImageView.

    描述: 

    显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

    android:adjustViewBounds
     是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。
     
    android:cropToPadding
     是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:


    android:maxHeight
     设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

    1) 设置setAdjustViewBounds为true;

    2) 设置maxWidth、MaxHeight;

    3) 设置设置layout_width和layout_height为wrap_content。
     
    android:maxWidth
     设置View的最大宽度。同上。
     
    android:scaleType
     设置图片的填充方式。

    android:src
     设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)
     
    android:tint
     将图片渲染成指定的颜色。

    **************************************************************************************************

    其他控件相对容易

    布局代码:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     tools:context=".MainActivity" >
     7 
     8     <ImageView
     9         android:id="@+id/iv_icon"
    10         android:layout_width="fill_parent"
    11         android:layout_height="0dip"
    12         android:layout_weight="1" />
    13 
    14     <LinearLayout
    15         android:layout_width="fill_parent"
    16         android:layout_height="wrap_content"
    17         android:orientation="horizontal" >
    18 
    19         <EditText
    20             android:id="@+id/et_url"
    21             android:layout_width="0dip"
    22             android:text="@string/http_img2_imgtn_bdimg_com_it_u_3128967132_3110899866_fm_11_gp_0_jpg"
    23             android:layout_height="wrap_content"
    24             android:singleLine="true"
    25             android:layout_weight="1" />
    26 
    27         <Button
    28             android:id="@+id/btn_submit"
    29             android:layout_width="wrap_content"
    30             android:layout_height="wrap_content"
    31             android:text="Go"
    32             android:textSize="20sp" />
    33     </LinearLayout>

    PS:换图片链接时不知道为什么会报错,需要把链接地址写入strings.xml中国际化调用,原本的地址并没有写入

    在主函数中用到了bitmap,可以把流变为图片(还不太懂内在含义,先整理出来)

    ********************************************************************************************************

    bitmap相关知识转自:http://yelinsen.iteye.com/blog/844711

    一、Bitmap转Drawable  (drawable简介:http://blog.csdn.net/yuzhiyuxia/article/details/8806488)
            Bitmap bm=xxx; //xxx根据你的情况获取 
            BitmapDrawable bd=BitmapDrawable(bm); 
            Android开发网提示因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 
    二、 Drawable转Bitmap 
            转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输 出流,最终还可以保存成为jpg和png的文件。 
            Drawable d=xxx; //xxx根据自己的情况获取drawable 
            BitmapDrawable bd = (BitmapDrawable) d; 
            Bitmap bm = bd.getBitmap(); 
            最终bm就是我们需要的Bitmap对象了。 

    Bitmap 相关 

    1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: 

    * png图片 如:R.drawable.ic_call_log_list_incoming_call 

    1 Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_call_log_list_incoming_call);    
    2   
    3 Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_call_log_list_incoming_call);   

    * 图像文件 如: /sdcard/dcim/ic_call_log_list_incoming_call.jpeg 

    1 Bitmap bmp = BitmapFactory.decodeFile("/sdcard/dcoim/ic_call_log_list_incoming_call.jpeg")    
    2 Bitmap bmp = BitmapFactory.decodeFile("/sdcard/dcoim/ic_call_log_list_incoming_call.jpeg")   

    2. Bitmap 相关应用 

    - 本地保存 即 把 Bitmap 保存在sdcard中 

    * 创建目标文件的File 

    1 File fImage = new File("/sdcard/dcim","ic_call_log_list_incoming_call.jpeg");     
    2     
    3 FileOutputStream iStream = new FileOutputStream(fImage);    
    4   
    5 File fImage = new File("/sdcard/dcim","ic_call_log_list_incoming_call.jpeg");  
    6   
    7 FileOutputStream iStream = new FileOutputStream(fImage);   

    * 取出Bitmap oriBmp 

    1 oriBmp.compress(CompressFormat.JPEG, 100, iStream);    
    2   
    3 oriBmp.compress(CompressFormat.JPEG, 100, iStream);   

    - 得到网路图片 

    定义网络图片对应的BufferedInputStream 

     1 //图片的链接地址     
     2 String icoURI = "http://202.140.96.134:8080/FS-RSS/img/ic_call_log_list_incoming_call.png";     
     3     
     4 URL imgURL = new URL(iu);     
     5 URLConnection conn = imgURL.openConnection();     
     6                  
     7 conn.connect();     
     8 InputStream is = conn.getInputStream();     
     9                  
    10 BufferedInputStream bis = new BufferedInputStream(is);   
    11   
    12 //图片的链接地址  
    13 String icoURI = "http://202.140.96.134:8080/FS-RSS/img/RN.png";  
    14   
    15 URL imgURL = new URL(iu);  
    16 URLConnection conn = imgURL.openConnection();  
    17      
    18 conn.connect();  
    19 InputStream is = conn.getInputStream();  
    20      
    21 BufferedInputStream bis = new BufferedInputStream(is);   
    22   
    23 //下载之  
    24 Bitmap bmp = BitmapFactory.decodeStream(bis);    
    25   
    26 Bitmap bmp = BitmapFactory.decodeStream(bis);   
    27   
    28 //关闭Stream  
    29 bis.close();     
    30     
    31 is.close();    
    32    

    **********************************************************************************************************

    先贴上主函数代码,具体实现自己还需要明天继续研读

    MainActivity.java

      1 package com.itheima28.netphoto;
      2 
      3 import java.io.InputStream;
      4 import java.net.HttpURLConnection;
      5 import java.net.MalformedURLException;
      6 import java.net.URL;
      7 
      8 import javax.net.ssl.HttpsURLConnection;
      9 
     10 import android.os.Bundle;
     11 import android.os.Handler;
     12 import android.os.Message;
     13 import android.app.Activity;
     14 import android.graphics.Bitmap;
     15 import android.graphics.BitmapFactory;
     16 import android.util.Log;
     17 import android.view.Menu;
     18 import android.view.View;
     19 import android.view.View.OnClickListener;
     20 import android.widget.EditText;
     21 import android.widget.ImageView;
     22 import android.widget.Toast;
     23 
     24 public class MainActivity extends Activity implements OnClickListener {
     25 
     26     private static final String TAG = "MainActivity";
     27     protected static final int ERROR = 1;
     28     private EditText etUrl;
     29     private ImageView ivIcon;
     30     private final int SUCCESS = 0;
     31     
     32     private Handler handler = new Handler() {
     33 
     34         /**
     35          * 接收消息
     36          */
     37         @Override
     38         public void handleMessage(Message msg) {
     39             super.handleMessage(msg);
     40             
     41             Log.i(TAG, "what = " + msg.what);
     42             if(msg.what == SUCCESS) {    // 当前是访问网络, 去显示图片
     43                 ivIcon.setImageBitmap((Bitmap) msg.obj);        // 设置imageView显示的图片
     44             } else if(msg.what == ERROR) {
     45                 Toast.makeText(MainActivity.this, "抓取失败", 0).show();
     46             }
     47         }
     48     };
     49 
     50     @Override
     51     protected void onCreate(Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53         setContentView(R.layout.activity_main);
     54         
     55         ivIcon = (ImageView) findViewById(R.id.iv_icon);
     56         etUrl = (EditText) findViewById(R.id.et_url);
     57         
     58         findViewById(R.id.btn_submit).setOnClickListener(this);
     59     }
     60 
     61     @Override
     62     public void onClick(View v) {
     63         final String url = etUrl.getText().toString();
     64         
     65         new Thread(new Runnable() {
     66 
     67             @Override
     68             public void run() {
     69                 Bitmap bitmap = getImageFromNet(url);
     70 
     71 //                ivIcon.setImageBitmap(bitmap);        // 设置imageView显示的图片
     72                 if(bitmap != null) {
     73                     Message msg = new Message();
     74                     msg.what = SUCCESS;
     75                     msg.obj = bitmap;
     76                     handler.sendMessage(msg);
     77                 } else {
     78                     Message msg = new Message();
     79                     msg.what = ERROR;
     80                     handler.sendMessage(msg);
     81                 }
     82             }}).start();
     83         
     84     }
     85 
     86     /**
     87      * 根据url连接取网络抓去图片返回
     88      * @param url
     89      * @return url对应的图片
     90      */
     91     private Bitmap getImageFromNet(String url) {
     92         HttpURLConnection conn = null;
     93         try {
     94             URL mURL = new URL(url);    // 创建一个url对象
     95             
     96             // 得到http的连接对象
     97             conn = (HttpURLConnection) mURL.openConnection();
     98             
     99             conn.setRequestMethod("GET");        // 设置请求方法为Get
    100             conn.setConnectTimeout(10000);        // 设置连接服务器的超时时间, 如果超过10秒钟, 没有连接成功, 会抛异常
    101             conn.setReadTimeout(5000);        // 设置读取数据时超时时间, 如果超过5秒, 抛异常
    102             
    103             conn.connect();        // 开始链接
    104             
    105             int responseCode = conn.getResponseCode(); // 得到服务器的响应码
    106             if(responseCode == 200) {
    107                 // 访问成功
    108                 InputStream is = conn.getInputStream();    // 获得服务器返回的流数据
    109                 
    110                 Bitmap bitmap = BitmapFactory.decodeStream(is); // 根据 流数据 创建一个bitmap位图对象
    111                 
    112                 return bitmap;
    113             } else {
    114                 Log.i(TAG, "访问失败: responseCode = " + responseCode);
    115             }
    116         } catch (Exception e) {
    117             e.printStackTrace();
    118         } finally {
    119             if(conn != null) {
    120                 conn.disconnect();        // 断开连接
    121             }
    122         }
    123         return null;
    124     }
    125 }

    MainActivity2.java

     1 package com.itheima28.netphoto;
     2 
     3 import com.loopj.android.image.SmartImageView;
     4 
     5 import android.app.Activity;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.EditText;
    10 
    11 public class MainActivity2 extends Activity implements OnClickListener {
    12 
    13     private EditText etUrl;
    14     private SmartImageView mImageView;
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main2);
    20         
    21         etUrl = (EditText) findViewById(R.id.et_url);
    22         mImageView = (SmartImageView) findViewById(R.id.iv_icon);
    23         
    24         findViewById(R.id.btn_submit).setOnClickListener(this);
    25     }
    26 
    27     @Override
    28     public void onClick(View v) {
    29         
    30         // 1. 取出url, 抓取图片
    31         String url = etUrl.getText().toString();
    32         
    33         mImageView.setImageUrl(url);
    34     }
    35 }

    明天务必把照片查看的源码读懂参透!具体查看照片的实现有想法是将图片的链接做成字的超链接,点击字就能显示出来图片,具体实现还得再仔细琢磨,近期抓紧申请服务器、考虑上传图片到服务器和侧边栏的实现

    借用最近新偶像朱伟老师的一句话作为结束语:“少一些功利主义的追求,多一些不为什么的坚持。”

  • 相关阅读:
    container宽度
    NO.14 两个div并排,左边为绝对宽度,右边为相对宽度
    CSS3旋转图片效果收集
    背景图片问题
    CSS动画
    前端比较有用的网址
    JS如何判断IE和火狐与Chrome浏览器
    JAVA多线程面试题目
    JAVA多线程之Semaphore
    阿里多线程笔试题
  • 原文地址:https://www.cnblogs.com/brucekun/p/5406291.html
Copyright © 2011-2022 走看看