安卓 图片二次采样

Bitmap二次采样

第一次采样:获得缩放比例

第二次采样:根据缩放比例进行压缩
//代码如下

// An highlighted block
public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageone);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Bitmap bitmap = new MyTask().execute("http://img02.tooopen.com/images/20150507/tooopen_sy_122395947985.jpg").get();
                    imageView.setImageBitmap(bitmap);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    class MyTask extends AsyncTask<String,Object, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {

            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                if (urlConnection.getResponseCode() == 200){
                    InputStream is = urlConnection.getInputStream();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int len = -1;
                    while ((is.read(bytes))!=len){
                        os.write(bytes,0,bytes.length);
                    }

                    byte[] data = os.toByteArray();

                    //第一次采样
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeByteArray(data,0,data.length,options);
                    int outHeight = options.outHeight;
                    int outWidth  = options.outWidth;
                    int size = 1;
                    while (outHeight/size>150 || outWidth/size>150){
                        size*=2;
                    }

                    //二次采样
                    options.inJustDecodeBounds = false;
                    options.inSampleSize = size;
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length,options);
                    return  bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

SQLite数据库操作

//代码如下:

// An highlighted block
public class myOpenHelper extends SQLiteOpenHelper {

    public  myOpenHelper(Context context){
        super(context,"1702CYang",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Student(id integer primary key autoincrement,name var(10),sex varchar(10),birth integer,department varchar(30),address varchar(30))");
        db.execSQL("create table Score(id integer primary key autoincrement,stu_id integer,c_name varchar(10),grade integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

添加到listview中 ,查询,添加,删除,修改

//效果图
添加

在这里插入图片描述
删除
在这里插入图片描述
修改
在这里插入图片描述

// An highlighted block
public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayList<Student> students = new ArrayList<>();
    SQLiteDatabase db;
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        init();
        listView = findViewById(R.id.listview);

        myOpenHelper helper = new myOpenHelper(MainActivity.this);
        db = helper.getWritableDatabase();

    }
    

    public  void select(View view){
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
    }

    public  void delete(View view){
        students.clear();
        db.execSQL("delete from student where birth = 1986 ");
        addListView();
    }

    public  void insetr(View view){
        db.execSQL("insert into student values (null,'张老大','男',1986,'计算机系','北京市海淀区')");
        db.execSQL("insert into student values (null,'张老二','男',1986,'中文系','北京市昌平区')");
        db.execSQL("insert into student values (null,'张三','女',1986,'中文系','湖南省永州市')");
        db.execSQL("insert into student values (null,'李四','男',1986,'英语系','辽宁省阜新市')");
        db.execSQL("insert into student values (null,'王五','女',1991,'英语系','福建省厦门市')");
        db.execSQL("insert into student values (null,'王六','男',1998,'计算机系','山西省霍州市')");
        addListView();
    }

    private void addListView() {
        Cursor cursor =db.rawQuery("select * from student",null);
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String sex = cursor.getString(cursor.getColumnIndex("sex"));
            int birth = cursor.getInt(cursor.getColumnIndex("birth"));
            String department = cursor.getString(cursor.getColumnIndex("department"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            students.add(new Student(id,name,sex,birth,department,address));
        }

        myAdapter = new MyAdapter(MainActivity.this,students);
        listView.setAdapter(myAdapter);
    }

    public  void update(View view){
        students.clear();
        db.execSQL("update student set  birth = 2000 where name = ?",new String[]{"王六"});
        addListView();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值