编写移动设备名字到图片中设为壁纸

这个程序是以图片插入文字为主要的技术,不说,先插入代码讲解:

package com.example.hello.app;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class MainActivity extends ActionBarActivity {

    public TextView phversion;
    public TextView sysver;
    public TextView debugtext;
    public String degstr="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phversion=(TextView)findViewById(R.id.phver);
        sysver=(TextView)findViewById(R.id.sysver);
        debugtext=(TextView)findViewById(R.id.debugtext);
        phversion.setText("手机型号:"+Build.MODEL);
        sysver.setText("系统型号:"+Build.VERSION.RELEASE);
        String newstr="手机型号:"+Build.MODEL.toString()+" "+"  系统型号:Android "+Build.VERSION.RELEASE.toString();
        Bitmap scrbm=createnewbitmap(newstr);
        WallpaperManager wm=WallpaperManager.getInstance(this);
        try{
        wm.setBitmap(scrbm);
        }catch (IOException e){
            e.printStackTrace();
        }
        Toast.makeText(this,"设置成功!",Toast.LENGTH_SHORT).show();
        debugtext.setText(degstr);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public Bitmap createnewbitmap(String drawtext){

        DisplayMetrics scressdm=getResources().getDisplayMetrics();//获得屏幕的dm
        int sw=scressdm.widthPixels;
        int sh=scressdm.heightPixels;
        degstr+="sw:"+sw+","+"sh:"+sh+";";

        /*int[] color=new int[sw*sh];
        for(int i=0;i<sw*sh;i++)
            color[i]=Color.parseColor("#FFFFFF");*/
        Bitmap bmp = Bitmap.createBitmap(sw, sh, Bitmap.Config.ARGB_8888);
        //Bitmap newbit=Bitmap.createBitmap(color,sw,sh,Bitmap.Config.ARGB_4444);
        Bitmap newbit= BitmapFactory.decodeResource(getResources(),R.drawable.bmg).copy(Bitmap.Config.ARGB_4444,true);
        Canvas canvasTmp = new Canvas(bmp);
        Paint p = new Paint();
        canvasTmp.drawBitmap(newbit,new Rect(0,0,newbit.getWidth(),newbit.getHeight()),new Rect(0,0,sw,sh),p);

        Typeface font = Typeface.create("", Typeface.BOLD);
        p.setColor(Color.parseColor("#EE1289"));
        p.setAntiAlias(true);
        p.setTypeface(font);
        p.setTextSize(sw/20);
        canvasTmp.drawText(drawtext, 0, sh/2, p);
        canvasTmp.save(Canvas.ALL_SAVE_FLAG);
        bitmaptojpg(bmp);
        degstr+="写字成功";
        return bmp;
    }

    public void bitmaptojpg(Bitmap bp){
        File bpfile=new File("/sdcard/hello/picture/newbmg.png");
        if(!bpfile.getParentFile().exists())//路径不存在的话,传建路径
            bpfile.getParentFile().mkdirs();
        FileOutputStream out;
        try {
            out=new FileOutputStream(bpfile);
            if(bp.compress(Bitmap.CompressFormat.PNG,70,out)){
                out.flush();
                out.close();
                Toast.makeText(this,"!!!!图片保存成功!!!!",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "****图片保存失败****", Toast.LENGTH_SHORT).show();
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}



首先先看生成bitmap图片的方法:


public Bitmap createnewbitmap(String drawtext){

        DisplayMetrics scressdm=getResources().getDisplayMetrics();//获得屏幕的dm
        int sw=scressdm.widthPixels;
        int sh=scressdm.heightPixels;
        degstr+="sw:"+sw+","+"sh:"+sh+";";

        /*int[] color=new int[sw*sh];
        for(int i=0;i<sw*sh;i++)
            color[i]=Color.parseColor("#FFFFFF");*/
        Bitmap bmp = Bitmap.createBitmap(sw, sh, Bitmap.Config.ARGB_8888);
        //Bitmap newbit=Bitmap.createBitmap(color,sw,sh,Bitmap.Config.ARGB_4444);
        Bitmap newbit= BitmapFactory.decodeResource(getResources(),R.drawable.bmg).copy(Bitmap.Config.ARGB_4444,true);
        Canvas canvasTmp = new Canvas(bmp);
        Paint p = new Paint();
        canvasTmp.drawBitmap(newbit,new Rect(0,0,newbit.getWidth(),newbit.getHeight()),new Rect(0,0,sw,sh),p);

        Typeface font = Typeface.create("", Typeface.BOLD);
        p.setColor(Color.parseColor("#EE1289"));
        p.setAntiAlias(true);
        p.setTypeface(font);
        p.setTextSize(sw/20);
        canvasTmp.drawText(drawtext, 0, sh/2, p);
        canvasTmp.save(Canvas.ALL_SAVE_FLAG);
        bitmaptojpg(bmp);
        degstr+="写字成功";
        return bmp;
    }
在注释中
</pre><pre>
 
 

new int[sw*sh]为颜色,若想自己设定颜色的话,可以自己修改里面的值,颜色是从左到右,从上到下(记得应该是,不行可以看看文档)设的

Bitmap bmp = Bitmap.createBitmap(sw, sh, Bitmap.Config.ARGB_8888);为Canvas做一个背景,其余的都图片啊什么的填上去,sw,和sh我这里是屏幕的宽度(因为我要做的是壁纸),可以自己设定自己想要的大小;而第三个参数是位图质量的问题了有几个可选的,可以自己试试看

Bitmap newbit= BitmapFactory.decodeResource(getResources(),R.drawable.bmg).copy(Bitmap.Config.ARGB_4444,true);这个要注意了,一定要写copy那一溜,,,这是获取资源,android不能直接修改资源,会报错一定要复制出一个来之后建画笔,把图片画上canvasTmp.drawBitmap(newbit,new Rect(0,0,newbit.getWidth(),newbit.getHeight()),new Rect(0,0,sw,sh),p);drawBitmap有多种重载,看参数,我因为是固定背景寬高,而图片的大小不一定和背景一样,因此用这个,其中第二个参数是从源那里取哪一快出来(Rect中的参数是左上右下四个),第三个是放到目地的那一块地方,源、目的大小不同的话会自动裁剪或者填充。

接下来设置文字的字体、颜色、大小等相信都能看懂p.setAntiAlias(true);消除锯齿好象是

接下来看保存图片的:

public void bitmaptojpg(Bitmap bp){
        File bpfile=new File("/sdcard/hello/picture/newbmg.png");
        if(!bpfile.getParentFile().exists())//路径不存在的话,传建路径
            bpfile.getParentFile().mkdirs();
        FileOutputStream out;
        try {
            out=new FileOutputStream(bpfile);
            if(bp.compress(Bitmap.CompressFormat.PNG,70,out)){
                out.flush();
                out.close();
                Toast.makeText(this,"!!!!图片保存成功!!!!",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "****图片保存失败****", Toast.LENGTH_SHORT).show();
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

先新建File,记住药看有没有父路径,没有要新建的。。mkdir()和mkdirs()不同之处在于:mkdir()创建此抽象路径名称指定的目录(及只能创建一级的目录,且需要存在父目录) mkdirs()创建此抽象路径指定的目录,包括所有必须但不存在的父目录。之后保存文件,需要权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

再看设置壁纸:

 WallpaperManager wm=WallpaperManager.getInstance(this);
        try{
        wm.setBitmap(scrbm);
        }catch (IOException e){
            e.printStackTrace();
        }
        Toast.makeText(this,"设置成功!",Toast.LENGTH_SHORT).show();
        debugtext.setText(degstr);

看看就懂,其中也要权限<uses-permission android:name="android.permission.SET_WALLPAPER" />


以下为转:

用android的canvas drawText的时候,即使text包行\r\n,画出来的仍然不会换行。

这时需要用到TextPaint


TextPaint textPaint = new TextPaint();  
textPaint.setARGB(0xFF, 0, 0, 0);  
textPaint.setTextSize(20.0F);  
textPaint.setAntiAlias(true);  
StaticLayout layout = new StaticLayout("abc\r\n123", textPaint, 300,  
        Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);  
cv.save();  
cv.translate(20, 20);//从20,20开始画  
layout.draw(cv);  
cv.restore();//别忘了restore


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值