栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

姓氏制作成头像

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

姓氏制作成头像

package com.atomic.moretool;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

public class NameToHeader extends AppCompatActivity {
    private TextView saveBgColor,saveTxtColor;
    private EditText nametoHeader,nameH,picW,picH;
    private ImageView showHeader;
    private final String[] strs={"红色","黄色","蓝色","黑色","绿色","灰色","白色"};
    private final String[] strings={"#ff0000","#ffff00","#00ffff","#000000","#00ff00","#d9d9d9","#ffffff"};

    //
    //        选择颜色
    //        "#ff0000" 
    //        "#ffff00" 
    //        "#00ffff" 
    //        "#000000" 
    //        "#00ff00" 
    //        "#d9d9d9" 
    //        "#ffffff" 
    //    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_to_header);
        init();
    }

    private void init() {
        saveBgColor=findViewById(R.id.save_bg_color);
        saveTxtColor=findViewById(R.id.save_txt_color);
        nametoHeader=findViewById(R.id.name_to_header);
        Button nametoBtn = findViewById(R.id.name_to_btn);
        Button saveName=findViewById(R.id.save_name);
        showHeader=findViewById(R.id.show_header);
        nameH=findViewById(R.id.name_h);
        picW=findViewById(R.id.pic_w);
        picH=findViewById(R.id.pic_h);
        nametoHeader.setSelectAllOnFocus(true);
        nameH.setSelectAllOnFocus(true);
        picW.setSelectAllOnFocus(true);
        picH.setSelectAllOnFocus(true);
        //声明一个下拉列表的数组适配器
        ArrayAdapter adapter=new ArrayAdapter<>(this,R.layout.spinner_item_select,strs);
        //设置数组适配器的布局样式
        adapter.setDropDownViewResource(R.layout.spinner_item_dropdown);
        //从布局文件中获取下拉框
        Spinner chooseColor = findViewById(R.id.spinner);
        //设置下拉框的标题
        chooseColor.setprompt("选择背景颜色");
        //设置下拉框的数组适配器
        chooseColor.setAdapter(adapter);
        chooseColor.setSelection(0);
        chooseColor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                saveBgColor.setText(strings[i]);

            }

            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
        Spinner choosetxtc = findViewById(R.id.spinner_text);
        choosetxtc.setAdapter(adapter);
        choosetxtc.setSelection(6);
        chooseColor.setprompt("选择文字颜色");
        choosetxtc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                saveTxtColor.setText(strings[i]);

            }

            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
        nametoBtn.setOnClickListener(view->{
            String name=nametoHeader.getText().toString().trim();
            String nameh=nameH.getText().toString().trim();
            String picw=picW.getText().toString().trim();
            String pich=picH.getText().toString().trim();
            String bgColor=saveBgColor.getText().toString().trim();
            String txtColor=saveTxtColor.getText().toString().trim();
            if (!name.equals("") && !nameh.equals("") && !picw.equals("") && !pich.equals("") &&!bgColor.equals("") && !txtColor.equals("")){
                Log.i("tag","选择了背景颜色"+bgColor);
                Log.i("tag","选择了文字颜色"+txtColor);
                //新建背景
                Bitmap bg=Bitmap.createBitmap(Integer.parseInt(picw),Integer.parseInt(pich),Bitmap.Config.ARGB_8888);
                Canvas bgCanvas=new Canvas(bg);
                bgCanvas.drawColor(Color.parseColor(bgColor));
                //文字转bitmap并新建
                try {
                    Bitmap txt=stringtoBitmap(Float.parseFloat(nameh),name,txtColor);
                    //将文字画在背景上
                    int left=(Integer.parseInt(picw)-Integer.parseInt(nameh))/2;
                    int top=(Integer.parseInt(pich)-Integer.parseInt(nameh))/2-Integer.parseInt(nameh)/10;
                    bgCanvas.drawBitmap(txt,left,top,null);
                    showHeader.setImageBitmap(bg);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else{
                Toast.makeText(this, "输入宽高与姓氏,选择背景与文字颜色", Toast.LENGTH_SHORT).show();
            }
        });
        saveName.setOnClickListener(view -> {
            try {
                if (showHeader.getDrawable() !=null){
                    Bitmap bm =((BitmapDrawable) showHeader.getDrawable()).getBitmap();
                    String savepath = Environment.getExternalStorageDirectory().getAbsolutePath().trim() + "/Atomic/" +nametoHeader.getText().toString().trim()+new Random().nextInt() +".jpg";
                    File file = new File(savepath);
                    if (!file.exists()) {
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    }
                    FileOutputStream fos = new FileOutputStream(file);

                    bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    Toast.makeText(this, "保存到Atomic文件夹", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "先制作", Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        });
    }
    //字符串转bitmap
    public static Bitmap stringtoBitmap(float textSize, String text,String txtColor) {
        Paint paint = new Paint();
        paint.setTextSize(textSize);
        paint.setTextAlign(Paint.Align.LEFT);
        //paint.setColor(Color.BLACK);
        paint.setColor(Color.parseColor(txtColor));
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
        int width = (int)paint.measureText(text);
        int height = fm.descent - fm.ascent;
        //int height=fm.bottom-fm.top;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawText(text, 0, fm.leading - fm.ascent, paint);
        canvas.save();
        return bitmap;
    }
}




    
    
    
    
    
    
    
    
    
        
        
        
    
    
        
        
        
    
    
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704597.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号