添加闪烁到自定义列表视图项?

时间:2015-04-22 13:58:45

标签: android listview border android-animation

我创建了一个listview包含多个项目,并为这些项目添加了边框,因此它看起来像这样:

enter image description here

由于这个自定义边框,项目的原始闪烁被阻止,因此它不会出现,并且我试图通过动画添加眨眼但是有太多的延迟和太多的工作主线程,我也在线程中使用了动画,但它与延迟的东西是一样的,这迫使我忘记动画所以无论如何我可以使项目在点击而不使用动画或使用它时闪烁但是在有效的方式和我在onItemClickListener中使用闪烁动画的方式,你可以在下面的代码中找到它: 处理眨眼的onItemClickListener

dataList.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, final View arg1,
                        final int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    tvWarningNoEmp.setVisibility(View.GONE);
                    // adding blink

                                final Animation animation = new AlphaAnimation(
                                        1, 0); // Change
                                // alpha
                                // from
                                // fully
                                // visible
                                // to
                                // invisible
                                animation.setDuration(100); // duration - half a
                                                            // second
                                animation
                                        .setInterpolator(new LinearInterpolator()); // do
                                // not
                                // alter
                                // animation
                                // rate
                                animation.setRepeatCount(Animation.INFINITE); // Repeat
                                // animation
                                // infinitely
                                animation.setRepeatMode(Animation.REVERSE);
                                arg1.startAnimation(animation);
                                // blink one time timer
                                new CountDownTimer(100, 100) {// CountDownTimer(edittext1.getText()+edittext2.getText())
                                                                // also parse it to long

                                    public void onTick(long millisUntilFinished) {

                                        // here you can have your logic to set text to
                                        // edittext
                                    }

                                    public void onFinish() {
                                        arg1.clearAnimation();
                                    }
                                }.start();



                                // set margins for tickets buttons
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                        LayoutParams.WRAP_CONTENT,
                                        LayoutParams.WRAP_CONTENT);
                                params.setMargins(0, 0, 30, 0);
                                bAssign.setLayoutParams(params);
                                LinearLayout.LayoutParams paramsO = new LinearLayout.LayoutParams(
                                        LayoutParams.WRAP_CONTENT,
                                        LayoutParams.WRAP_CONTENT);
                                paramsO.setMargins(110, 0, 0, 0);
                                bEmpChat.setLayoutParams(paramsO);
                                noEmpLayout.setVisibility(LinearLayout.GONE);
                                empLayout.setVisibility(LinearLayout.VISIBLE);
                                Employee item = adapter.getItem(arg2);
                                ivEmpIcon.setBackgroundResource(R.drawable.free);
                                tvEmpName.setText(" " + item.getEmpName());
                                tvEmpDetails.setText(" " + item.getEmpDetails());
                                empLongitude = item.getEmpLongitude();
                                empLatitude = item.getEmpLatitude();
                                CameraPosition cameraPosition = new CameraPosition.Builder()
                                        .target(new LatLng(empLatitude, empLongitude))
                                        .zoom(15).build();
                                googleEmpMap.animateCamera(CameraUpdateFactory
                                        .newCameraPosition(cameraPosition));
                                MarkerOptions marker = new MarkerOptions().position(
                                        new LatLng(empLatitude, empLongitude)).title(
                                        tvEmpName.getText().toString());
                                marker.icon(BitmapDescriptorFactory
                                        .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                                googleEmpMap.addMarker(marker);
                                // based on item add info to intent
                                // hide button directions
                                bEmpDirections.setVisibility(View.GONE);
                                // current address
                                Geocoder geocoder;
                                List<Address> addresses;
                                geocoder = new Geocoder(getApplicationContext(), Locale
                                        .getDefault());
                                try {
                                    addresses = geocoder.getFromLocation(empLatitude,
                                            empLongitude, 1);
                                    String street = addresses.get(0).getAddressLine(0);
                                    String state = addresses.get(0).getAdminArea();
                                    String country = addresses.get(0).getCountryName();
                                    tvEmpCurrentAdd.setText(" " + country + "-" + state
                                            + "-" + street + " st.");
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();}





                }

            });

ListView布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/redborder" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
<LinearLayout
   android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >
        <Button
            android:id="@+id/btnBack"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/back" />

        <TextView
            android:id="@+id/tvWarningNoEmp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="choose an employee from here to assign this problem to!"
            android:textColor="#FF0000" 
            android:layout_marginLeft="400dp"
            android:visibility="gone"/>

</LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="40dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"

                android:layout_weight="2"
                android:background="@drawable/pepsiborder"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/blueborder"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <ImageView
                        android:id="@+id/TicketIcon"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="0.1" />

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="0.7"
                        android:gravity="center_vertical"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/ticketCat"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="7dp"
                            android:gravity="center_vertical"
                            android:textColor="#D3D9FF"
                            android:textSize="17sp" />
                    </LinearLayout>
                </LinearLayout>

                <fragment
                    android:id="@+id/ticketMap"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="State:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/ticketState"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Ticket No:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/ticketNo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView12"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Report Date:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/ticketReportDate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />
                    </LinearLayout>


                        <TextView
                            android:id="@+id/textView13"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Details:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/ticketDetails"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />

                </LinearLayout>
</ScrollView>
                <LinearLayout
                    android:id="@+id/llBtn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="170dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/blueborder"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <Button
                        android:id="@+id/btnTicketDirections"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:background="@drawable/directions" />

                    <Button
                        android:id="@+id/btnSolve"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:background="@drawable/solve" />

                    <Button
                        android:id="@+id/btnOther"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:background="@drawable/other" />


                </LinearLayout>
            </LinearLayout>
 <LinearLayout
     android:id="@+id/llListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="40dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="2"
                android:background="@drawable/pepsiborder"
                android:orientation="vertical" >
     <TextView
        android:id="@+id/textView124"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#D3D9FF"
        android:text="Free Employees"
        android:textSize="30sp"
        android:gravity="left"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="20dp"

            >

    <ListView
        android:id="@+id/listTic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_marginTop="20dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp" >

    </ListView>
  </LinearLayout>

 </LinearLayout>
 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="2"
                android:id="@+id/noempLayout"
                android:background="@android:color/transparent"
                android:orientation="vertical" >
     <TextView 
         android:text="Please select free Employee to handle this problem!"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:gravity="center"
         android:layout_marginTop="300dp"
         />
 </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="40dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="2"
                android:id="@+id/empLayout"
                android:background="@drawable/pepsiborder"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/blueborder"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <ImageView
                        android:id="@+id/empIcon"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="0.1" />

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="0.7"
                        android:gravity="center_vertical"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/empName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="7dp"
                            android:gravity="center_vertical"
                            android:textColor="#D3D9FF"
                            android:textSize="17sp" />
                    </LinearLayout>
                </LinearLayout>

                <fragment
                    android:id="@+id/empMap"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView33"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Current Address:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/empAddress"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />
                    </LinearLayout>



                <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView22"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Details:"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#D3D9FF" />

                        <TextView
                            android:id="@+id/empDetails"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#D3D9FF" />
                    </LinearLayout>
                    </LinearLayout>
                    <LinearLayout
                    android:id="@+id/llBtn2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginTop="220dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/blueborder"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <Button
                        android:id="@+id/btnEmpDirections"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:layout_gravity="center"
                        android:background="@drawable/directions" />

                    <Button
                        android:id="@+id/btnEmpChat"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:layout_gravity="center"
                        android:background="@drawable/chat" />

                    <Button
                        android:id="@+id/btnEmpAssign"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_weight="1"
                        android:layout_gravity="center"
                        android:background="@drawable/assign" />


                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

ListView项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="280dp"
    android:layout_height="90dp"
    android:orientation="horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="10dp"
    android:padding="10dp"
     >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_weight="0.1"
        android:gravity="center_vertical" 

         />
    <LinearLayout 
      android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_weight="0.7"
    android:gravity="center_vertical"

        >
    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="17sp"
        android:textColor="#D3D9FF"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="7dp"
           />
     <TextView
        android:id="@+id/txtTitle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_gravity="center_vertical"
        android:textSize="13sp"
        android:textColor="#D3D9FF"
        android:layout_marginLeft="7dp"
           />
      </LinearLayout>
</LinearLayout>

Border.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
       >
   <stroke
        android:width="2dip"
        android:color="#8D8D8D" />
   <corners android:radius="10dip"/>
   <solid android:color="#464A4F" />
</shape>

真正感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

如果您希望视图闪烁:

public void blink(final View v) {
        v.setBackgroundColor(Color.DKGRAY);
        v.animate().setDuration(25).alpha(0).withEndAction(new Runnable() {
            @Override
            public void run() {
                v.setBackgroundResource(R.drawable.standard_key_normal);
                v.setAlpha(1);
            }
        });
    }

将您的视图发送到此功能,将背景替换为您想要的任何可绘制或颜色资源。如果您想要多次闪烁,请将其置于for循环中。

答案 1 :(得分:1)

好吧我弄清楚如何减少延迟并添加闪烁效果而不使用动画和一些它如何对我工作,并没有更多的延迟,我做的是我已经将列表的线性布局更改为相对和减少嵌套布局的数量显着然后我添加了这个方法:

private void blink(final View v){
    final Handler handler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
        int timeToBlink = 1000;    //in milissegunds
        try{Thread.sleep(timeToBlink);}catch (Exception e) {}
            handler.post(new Runnable() {
                @Override
                    public void run() {

                    if(v == View.VISIBLE){
                        v.setVisibility(View.INVISIBLE);
                    }else{
                        v.setVisibility(View.VISIBLE);
                    }
                    blink();
                }
                });
            }
        }).start();

然后我在onItemClickListener()内使用了它 - &gt; blink(arg1); 对我来说工作得很好,也让当前地址占用太多,所以我使用Async所以现在没有任何延迟: Async用作内部类:

private class LongOperation extends AsyncTask<String, Void, String> {
        String all;
        @Override
        protected String doInBackground(String... params) {
            // current address
            Geocoder geocoder;
            List<Address> addresses;

            geocoder = new Geocoder(getApplicationContext(), Locale
                    .getDefault());
            try {
                addresses = geocoder.getFromLocation(empLatitude,
                        empLongitude, 1);
                String street = addresses.get(0).getAddressLine(0);
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                all="" + country + "-" + state
                        + "-" + street + " st.";

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();}
            return all;
        }

        @Override
        protected void onPostExecute(String result) {
            tvEmpCurrentAdd.setText(all);
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

然后我在onItemClickListener内调用它: new LongOperation().execute(); 希望这能帮助其他有类似问题的人。