我正在尝试创建一个通用框架函数,使得任何Drawable在按下/聚焦/选择/等时都会突出显示.
我的函数接受一个Drawable并返回一个StateListDrawable,其中默认状态是Drawable本身,而状态for android.R.attr.state_pressed是相同的drawable,只需使用过滤器setColorFilter.
我的问题是我无法克隆drawable并使用过滤器创建一个单独的实例.这是我想要实现的目标:
StateListDrawable makeHighlightable(Drawable drawable)
{
StateListDrawable res = new StateListDrawable();
Drawable clone = drawable.clone(); // how do I do this??
clone.setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
res.addState(new int[] {android.R.attr.state_pressed}, clone);
res.addState(new int[] { }, drawable);
return res;
}
Run Code Online (Sandbox Code Playgroud)
如果我没有克隆,那么过滤器显然适用于这两种状态.我试过玩,mutate()但它没有帮助..
有任何想法吗?
更新:
接受的答案确实克隆了一个可绘制的.这对我没有帮助,因为我的一般功能在一个不同的问题上失败了.看起来当你向StateList添加一个drawable时,它会丢失所有的过滤器.
我只是想知道是否有人知道在互联网上开发状态机的一些很好的教程.还是电子书?
我开始在状态机上工作,只需要一些通用的东西让我开始.
在Scala中,我可以在编译时强制执行类型相等.例如:
case class Foo[A,B]( a: A, b: B )( implicit ev: A =:= B )
scala> Foo( 1, 2 )
res3: Foo[Int,Int] = Foo(1,2)
scala> Foo( 1, "2" )
<console>:10: error: Cannot prove that Int =:= java.lang.String.
Run Code Online (Sandbox Code Playgroud)
有没有办法强制执行类型A和类型B应该是不同的?
从官方教程参考中我还没有完全理解未来的一个方面.http://docs.scala-lang.org/overviews/core/futures.html
scala中的期货是否有某种内置的超时机制?假设下面的示例是一个5千兆字节的文本文件......"Implicits.global"的隐含范围是否会导致onFailure以非阻塞方式触发或者是否可以定义?没有某种默认的超时时间,这是否意味着它既不会成功也不会失败?
import scala.concurrent._
import ExecutionContext.Implicits.global
val firstOccurence: Future[Int] = future {
val source = scala.io.Source.fromFile("myText.txt")
source.toSeq.indexOfSlice("myKeyword")
}
firstOccurence onSuccess {
case idx => println("The keyword first appears at position: " + idx)
}
firstOccurence onFailure {
case t => println("Could not process file: " + t.getMessage)
}
Run Code Online (Sandbox Code Playgroud) 在Java中,我有时使用类变量为每个新实例分配唯一的ID.我做的事情
public class Foo {
private static long nextId = 0;
public final long id;
public Foo() {
id = nextId;
nextId++;
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Scala中执行此操作?
我有一个多项目SBT项目,看起来像SBT doc上的例子:
import sbt._
import Keys._
object HelloBuild extends Build {
lazy val root = Project(id = "hello",
base = file(".")) aggregate(foo, bar)
lazy val foo = Project(id = "hello-foo",
base = file("foo"))
lazy val bar = Project(id = "hello-bar",
base = file("bar"))
}
Run Code Online (Sandbox Code Playgroud)
因为root它只是一个聚合两个子项目的虚拟项目,我想避免包生成(和工件发布),但仍然为两个子项目生成包(和发布).
有没有一种简单的方法来实现它?
我正在通过AngularJS教程.Angular使用它自己的JS路由机制来允许单页应用程序.Angular的示例路由文件如下所示:
angular.module('phonecat', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/phones', {templateUrl: '/partials/phone-list', controller: PhoneListCtrl}).
when('/phones/:phoneId', {templateUrl: 'partials/phone-detail', controller: PhoneDetailCtrl}).
otherwise({redirectTo: '/phones'});
}]);
Run Code Online (Sandbox Code Playgroud)
我试图找到一个存储我的部分(Angular特定HTML文件)的好地方.理想情况下,我喜欢在Play中模拟它们的能力(即将它们作为*.scala.html文件).我可以使用如下的播放路径文件来完成此操作:
GET /partials/phone_index controllers.Application.phone_index
Run Code Online (Sandbox Code Playgroud)
我基本上偏爱/像这样的控制器动作:
def phone_index = Action {
Ok(views.html.partials.phone_index())
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的解决方案是两个理想的组合:
有任何想法吗?
我正在尝试迁移一个小项目,用Guice替换一些工厂(这是我的第一个Guice试验).但是,在尝试注射仿制药时,我陷入困境.我设法提取了一个带有两个类和一个模块的小玩具示例:
import com.google.inject.Inject;
public class Console<T> {
private final StringOutput<T> out;
@Inject
public Console(StringOutput<T> out) {
this.out = out;
}
public void print(T t) {
System.out.println(out.converter(t));
}
}
public class StringOutput<T> {
public String converter(T t) {
return t.toString();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(StringOutput.class);
bind(Console.class);
}
public static void main(String[] args) {
Injector injector = Guice.createInjector( new MyModule() );
StringOutput<Integer> out = injector.getInstance(StringOutput.class); …Run Code Online (Sandbox Code Playgroud) 我尝试使用-optimise版本2.7.7的scalac .那时我从来没有得到任何性能改进,但编译花费的时间更长.
Scala 2.9.0中的情况更好吗?国旗目前涵盖哪些优化措施?
compiler-construction performance scala compiler-optimization
Web应用程序框架(如sinatra(ruby),play(scala),lift(scala))生成一个侦听特定端口的Web服务器.
我知道有一些原因,如安全性,群集以及在某些情况下的性能,可能会导致我在我的Web应用程序前面使用Apache Web服务器.
根据您的经验,您有任何理由吗?