android 动态 添加删除 控件 图片

1、点击选择图片,打开手机相册库。

 

2、选择图片

3、效果图 添加图片的同时  添加删除按钮

 

4、添加2张图片后  ,点击删除

5、图片和 按钮删除

源码如下:有看不懂的可以联系   QQ :547613155

 

<?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"  
    >  
<TextView    
    android:id="@+id/url" 
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    
    />  
   <Button   
        android:id="@+id/b01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />

    <TableLayout 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
            android:orientation="vertical"
    >
       <TableRow 
        android:id="@+id/tupian"
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content" 
        android:scrollbars="none"
        
        >

    </TableRow>
    <TableRow 
         android:id="@+id/shanchu"
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content" 
        android:scrollbars="none"
        
        >

    </TableRow>
    </TableLayout>
 
</LinearLayout> 
package com.examlpe.AAAAAyin;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TableRow;
import android.widget.TextView;




public class AAAAAyinActivity extends Activity {

    /** Called when the activity is first created. */
    private int index = 0;
    private TableRow tupian;
    private TableRow shanchu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.b01);  
        button.setText("选择图片");  
        button.setOnClickListener(new Button.OnClickListener(){  
            @Override  
            public void onClick(View v) {  
                Intent intent = new Intent();  
                /* 开启Pictures画面Type设定为image */  
                intent.setType("image/*");  
                /* 使用Intent.ACTION_GET_CONTENT这个Action */  
                intent.setAction(Intent.ACTION_GET_CONTENT);   
                /* 取得相片后返回本画面 */  
                startActivityForResult(intent, 1);  
                
            }  
              
        });  
 
    }  
      
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            Uri uri = data.getData();  
            Log.e("uri", uri.toString());  
            

            ContentResolver cr = this.getContentResolver();  
            try {  
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); 
                
             //   String str = "iv0"+Integer.toString(aa);
             //   ImageView imageView = (ImageView) findViewById(R.id.);  
                tupian = (TableRow) findViewById(R.id.tupian);
                shanchu = (TableRow) findViewById(R.id.shanchu);
                //tupian.addView(AddImageView(), 1);
                tupian.addView(AddImageView(), 100 , 100);
                shanchu.addView(AddButton(), 100 , 50);

                
                ImageView imageView1 = (ImageView) findViewById(index);
              
                ImageView(imageView1 , bitmap );
                
            } catch (FileNotFoundException e) {  
                Log.e("Exception", e.getMessage(),e);  
            }  
        }  
        super.onActivityResult(requestCode, resultCode, data);  
    } 
    //  动态添加  图片
    private void ImageView(ImageView imageView , Bitmap bitmap ) {
        
         Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
        imageView.setImageBitmap(mBitmap); 
    }
    protected View AddImageView() {
        index++;
        ImageView btn = new ImageView(this);
        btn.setId(index);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//        btn.setScaleType(FIT_XY);
        return btn;
    }
    protected View AddButton() {
    //    index1++;
        Button btn = new Button(this);
        btn.setId(index);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btn.setOnClickListener(new Button.OnClickListener(){  
          @Override  
          public void onClick(View v) {  

              int i = v.getId() ;
              shanchu.removeView(v) ;
              tupian.removeView(tupian.findViewById(i)) ;
          }  
            
        });
        btn.setText("删除");
        
        return btn;
    }


}

 

 

 

转载于:https://www.cnblogs.com/taishan/archive/2012/06/21/2557299.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值