编程学习网 > 编程语言 > Java > 用eclipse制作一个属于程序员浪漫的表白代码
2021
12-21

用eclipse制作一个属于程序员浪漫的表白代码

有多少人是借着真心话大冒险去和喜欢的人表白的看看,本文就教大家使用eclipse逗逗妹子,表白一下吧

先秀一下效果吧

可以使用手机进行录制屏幕或者转gif

开发工具使用eclipse

代码如下:

MainActivity.java




public class MainActivity extends ListActivity implements BaseSliderView.OnSliderClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化图片切换
        initChangePicture();
        //初始化音乐
        initMusic();
        //初始化文字展示
        initWord();
    }
    
    @Override
    public void onSliderClick(BaseSliderView slider) {
        Toast.makeText(this, slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
    }
    
    private SliderLayout mDemoSlider;
    private void initChangePicture(){
         mDemoSlider = (SliderLayout)findViewById(R.id.slider);

         //两种方式加载数据
         
         //加载本地
         HashMap<String,String> url_maps = new HashMap<String, String>();
         url_maps.put("GitOnWay", "http://gitonway.blog.163.com/");
         
         //加载网络
         HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
         file_maps.put("love-A",R.drawable.a);
         file_maps.put("love-B",R.drawable.b);
         file_maps.put("love-C",R.drawable.c);
         file_maps.put("love-D", R.drawable.d);
         
         for(String name : file_maps.keySet()){
             TextSliderView textSliderView = new TextSliderView(this);
             // 初始化幻灯片页面
             textSliderView
                     .description(name)
                     .image(file_maps.get(name))
                     .setOnSliderClickListener(this);

             //添加要传递的数据
             textSliderView.getBundle()
                     .putString("extra",name);

            mDemoSlider.addSlider(textSliderView);
         }
         
//       幻灯片切换方式  
         mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
//       指示符位置  
         mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
//       定义指示器样式  
//       mDemoSlider.setCustomIndicator(your view);
//       幻灯片循环  
//       mDemoSlider.startAutoCycle();
//       停止循环
         mDemoSlider.stopAutoCycle();
//       设置指示器的显示与否  
         mDemoSlider.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
//       设置幻灯片的转化时间  
//       mDemoSlider.setSliderTransformDuration(5000, null);
//       用来自定义幻灯片标题的显示方式  
         mDemoSlider.setCustomAnimation(new DescriptionAnimation());
//       幻灯片切换时间  
         mDemoSlider.setDuration(3000);
         
//         实现随机切换
         TimerTask task = new TimerTask() {
             @Override
             public void run() {
                 Transformer[] tranformers = SliderLayout.Transformer.values();
                 Transformer transformer = tranformers[(int) (Math.random() * tranformers.length)];
                 mDemoSlider.setPresetTransformer(transformer);
             }
         };
         
         new Timer().schedule(task, 2000, 2000);
    }
    
    //res/raw中的音乐文件资源映射
    private Map<String, Integer> musicPath;
    //播放对象
      private MediaPlayer myMediaPlayer;
      //播放列表
      private List<String> myMusicList = new ArrayList<String>();
      //当前播放歌曲的索引
      private int currentListItem=0;
      //音乐的路径, 如果存在sd卡,则使用sd卡,否则使用内存中的data目录
      private static String MUSIC_PATH = hasSDCardMounted() ?  new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/hjz/")
                  : null;
      
      
    private void initMusic(){
        myMediaPlayer=new MediaPlayer();
        findView();
        musicList();
        listener();
        
        //自动播放第一首歌
        if(myMusicList.size() > 0){
            playMusic(MUSIC_PATH, myMusicList.get(currentListItem));
        }
    }
    
    public static boolean hasSDCardMounted() {
        String state = Environment.getExternalStorageState();
        if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
    
    //绑定音乐
    private void musicList(){
        try {
            File home = new File(MUSIC_PATH);
            //如果有sd卡,但是sd卡中没有指定的音乐文件夹,则采用项目中的音乐文件
            if(MUSIC_PATH == null || home.listFiles() == null) {//绑定 res/raw下的音乐文件
                MUSIC_PATH = null;
                musicPath = new HashMap<String, Integer>();
                musicPath.put("杨宗纬 - 一次就好.mp3", R.raw.yi_ci_jiu_hao);
                musicPath.put("霍建华,赵丽颖 - 不可说.mp3", R.raw.bu_ke_shuo);
                musicPath.put("川井憲次 - 孤独な巡礼.mp3", R.raw.gu_du_xun_li);
                myMusicList.addAll(musicPath.keySet());
            } else {
                Log.v("MUSIC_PATH", MUSIC_PATH);
                if(home.listFiles(new MusicFilter()).length>0){
                    for(File file:home.listFiles(new MusicFilter())){
                        myMusicList.add(file.getName());
                    }
                }
            }
            if(myMusicList.size() > 0) {
                ArrayAdapter<String> musicList = new ArrayAdapter<String>(MainActivity.this, R.layout.musicitme, myMusicList);
                setListAdapter(musicList);
            }
        } catch (Exception e) {
            Log.e("获取音乐文件出错:", e.toString());
        }
    }
    
    //获取按钮
   void findView(){
       viewHolder.start=(Button)findViewById(R.id.start);
       viewHolder.stop=(Button)findViewById(R.id.stop);
       viewHolder.next=(Button)findViewById(R.id.next);
       viewHolder.pause=(Button)findViewById(R.id.pause);
       viewHolder.last=(Button)findViewById(R.id.last);
   }
   
   
   //监听事件
   void listener(){
       //停止
       viewHolder.stop.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(myMediaPlayer.isPlaying()){
                myMediaPlayer.reset();
            }
        }
    });
       //开始
     viewHolder.start.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            if(myMusicList.size() == 0) return;
            playMusic(MUSIC_PATH, myMusicList.get(currentListItem));
        }
    });
       //下一首
       viewHolder.next.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            nextMusic();
        }
    });
       //暂停
       viewHolder.pause.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            if(myMusicList.size() == 0) return;
            if(myMediaPlayer.isPlaying()){
                myMediaPlayer.pause();
            }else{
                myMediaPlayer.start();
            }
        }
    });
       //上一首
    viewHolder.last.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            lastMusic();
        }
    });
       
   }
   
   //播放音乐 
   void playMusic(String basePath, String path){
      try { 
           if(basePath != null) {
               myMediaPlayer.reset();
               myMediaPlayer.setDataSource(basePath+path);
               myMediaPlayer.prepare();
           } else {
               myMediaPlayer.pause();
               myMediaPlayer.release();
               myMediaPlayer = MediaPlayer.create(MainActivity.this, musicPath.get(path));
           }
           myMediaPlayer.start();
           myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    nextMusic();
                }
            });
       } catch (Exception e) {
           Log.e("播放sd卡音乐失败", e.toString());
           e.printStackTrace();
       }
   }
   
   //下一首
   void nextMusic(){
       if(myMusicList.size() > 0) {
           if(++currentListItem>=myMusicList.size()){
               currentListItem=0;
           }
           playMusic(MUSIC_PATH, myMusicList.get(currentListItem));
       }
   }
   
   //上一首
   void lastMusic(){
       if(myMusicList.size() > 0) {
           if(currentListItem!=0) {
               playMusic(MUSIC_PATH, myMusicList.get(--currentListItem));
            }  else{
               playMusic(MUSIC_PATH, myMusicList.get(currentListItem=myMusicList.size()-1));
            }
       }
   }
   
   //当用户返回时结束音乐并释放音乐对象
       @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
           if(keyCode==KeyEvent.KEYCODE_BACK){
               myMediaPlayer.stop();
               myMediaPlayer.release();
               this.finish();
               return true;
           }
        return super.onKeyDown(keyCode, event);
    }
   
    //当选择列表项时播放音乐 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        currentListItem=position;
        playMusic(MUSIC_PATH, myMusicList.get(currentListItem));
    }
    
    //初始化文字展示
    private TextSurface textSurface;
    private void initWord(){
        LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayoutWord);//找到你要设透明背景的layout 的id 
        layout.getBackground().setAlpha(60);//0~255透明度值 
        textSurface = (TextSurface) findViewById(R.id.text_surface);
        textSurface.postDelayed(new Runnable() {
            @Override public void run() {
                show();
            }
        }, 1000);
    }
    
    private void show() {
        textSurface.reset();
        List<AnimationsSet> animationsSets = new ArrayList<AnimationsSet>();
        animationsSets.add(CookieThumperSample.getCookieThumperAnimations(getAssets()));
        animationsSets.addAll(SlideSample.getSlideAnimations(getContents()));
        textSurface.play(TYPE.SEQUENTIAL, animationsSets.toArray(new AnimationsSet[]{}));
        
        
//        ColorSample.play(textSurface);
//        AlignSample.play(textSurface);
//        Rotation3DSample.play(textSurface);
//        ScaleTextSample.run(textSurface);
//        ShapeRevealLoopSample.play(textSurface);
//        ShapeRevealSample.play(textSurface);
//        SlideSample.play(textSurface);
//        SurfaceScaleSample.play(textSurface);
//        SurfaceTransSample.play(textSurface);
    }
    
    private List<String> getContents(){
        List<String> contents = new ArrayList<String>();
        try{   
           //得到资源中的asset数据流  
           String fileName = "content.txt"; //文件名字   
           String res="";   
           InputStream in = getResources().getAssets().open(fileName);   
           int length = in.available();           
           byte [] buffer = new byte[length];          
           in.read(buffer);              
           in.close();  
           res = EncodingUtils.getString(buffer, "UTF-8");
           String[] strings = res.split("[,|,|\\.|。]");
           int len = strings.length/4 * 4;
           for(int i=0; i < len; ++i)
               contents.add(strings[i]);
        }catch(Exception e){   
          e.printStackTrace();
          Log.e("getContents", e.toString());
       }   
        return contents;
    }
}

activity_main.xml(页面布局)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gitonway.androidimagesliderdemo.activity.MainActivity">
    
    <LinearLayout android:id="@+id/LinearLayoutPicture"
        android:orientation="vertical"
        android:layout_weight="3"
        android:layout_height="fill_parent"
        android:layout_width="match_parent">
        <!-- 图片切换  -->
        <com.gitonway.androidimagesliderdemo.widget.imageslider.SliderLayout
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            custom:pager_animation="Accordion"
            custom:auto_cycle="true"
            custom:indicator_visibility="visible"
            custom:pager_animation_span="1100"/>
    
      <!--  <com.gitonway.androidimagesliderdemo.widget.imageslider.Indicators.PagerIndicator
            android:id="@+id/custom_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            custom:selected_color="#0095BF"
            custom:unselected_color="#55333333"
            custom:selected_drawable="@drawable/ic_launcher"
            custom:shape="oval"
            custom:selected_padding_left="6dp"
            custom:selected_padding_right="6dp"
            custom:unselected_padding_left="2dp"
            custom:unselected_padding_right="2dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            custom:selected_width="6dp"
            custom:selected_height="6dp"
            custom:unselected_width="6dp"
            custom:unselected_height="6dp"
            android:layout_marginBottom="20dp"
            />
        <com.gitonway.androidimagesliderdemo.widget.imageslider.Indicators.PagerIndicator
            android:id="@+id/custom_indicator2"
            style="@style/AndroidImageSlider_Corner_Oval_Orange"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            />  -->
    </LinearLayout>
    
    <!-- 文字展现  -->
    <LinearLayout android:id="@+id/LinearLayoutWord"
        android:layout_weight="3"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@android:color/background_dark"
        android:layout_width="match_parent">

        <com.textsurface.TextSurface
            android:id="@+id/text_surface"
            android:layout_height="fill_parent"
            android:layout_width="match_parent"/>
    
    </LinearLayout>
    
    <!-- 音乐播放器  -->

    <LinearLayout
        android:id="@+id/LinearLayoutMusic"
        android:layout_width="match_parent"
        android:layout_weight="4"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        
        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:scrollbars="vertical" />
        
        <LinearLayout
            android:id="@+id/bottomBtn"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:orientation="horizontal" >
    
            <Button   android:id="@+id/last"  
              android:background="@drawable/last"
              android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            
            <Button  android:id="@+id/stop"
              android:background="@drawable/stop"
              android:layout_marginLeft="10dp"
              android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
             
            <Button  android:id="@+id/start" 
              android:background="@drawable/start"
              android:layout_marginLeft="10dp"
              android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            
            <Button  android:id="@+id/pause" android:layout_width="wrap_content"
              android:background="@drawable/pause"
              android:layout_marginLeft="10dp"
              android:layout_height="wrap_content"></Button>
             
            <Button  android:id="@+id/next"
              android:background="@drawable/next"
              android:layout_marginLeft="10dp"
              android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

以上就是“用eclipse制作一个属于程序员浪漫的表白代码”的详细内容,想要了解更多表白代码欢迎持续关注编程学习网

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取